Enter Matching Bracket Using Text Expansion Trigger [Example]

MACRO:   Enter Matching Bracket Using Text Expansion Trigger [Example]


VER: 1.0    2016-11-29 ~~~

DOWNLOAD:

Enter Matching Bracket Using Text Expansion Trigger [Example].kmmacros (7.9 KB)

# Please read the SETUP below to configure for your use.

See Demo Below


Performance

I find this macro runs very fast (<< 1 sec) on my 2013 MBP-15R with 2.7Ghz.
It runs fast enough for me to continue typing immediately after typing the trigger, without any pause.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Auto-Type Closing Bracket, and Position Text Cursor Between Brackets

HOW TO USE:

  1. In a text field/document, simply type semicoln (;),
    followed by the OPEN Bracket, any of:
    [], {}, (), <>

MACRO SETUP

  • ENABLE this Macro

  • MACRO GROUP

    • Put Macro in a Macro Group that limits activation to only those apps where you want to use this macro
      • I recommend that you do NOT put in a Global Group, since some apps provide this feature internally
    • This Macro was uploaded using a Macro Group of "Examples", which activated ONLY for TextWrangler
      • You will need to change this, or move macro, to a Group that has your App(s) activated.
  • The macro comes preset with support for the following brackets:
    [], {}, (), <>

  • You can easily add other brackets by:

    • ADD the OPEN Bracket to the Typed Trigger and the Regular Expression in the Action "SEARCH: Get Open Bracket"
    • ADD the OPEN and CLOSE Brackets to the Action "Bracket List"
  • TRIGGER PREFIX

    • This macro uses the semicolon (;) as a prefix to the actual open bracket
    • You can change this to any other prefix, or even remove it (which I don't recommend), by changing:
    • Type String Trigger
    • Regular Expression in the Action "SEARCH: Get Open Bracket"

TAGS: @TE @Brackets @Example

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

ACTION COLOR CODES

  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Varibles or Clipboards

REQUIRES:

(1) Keyboard Maestro Ver 7.3+
(2) Yosemite (10.10.5)+


4 Likes

###Demo Using ; Prefix

https://dl.dropboxusercontent.com/u/7107976/KM/KM-7.3-Match-Bracket-Macro-Demo-AN.gif

###Demo With NO Prefix

https://dl.dropboxusercontent.com/u/7107976/KM/KM-7.3-Match-Bracket-Macro-No-Prefix-Demo-AN.gif

I’ve gotten fond of using %|% for Position Cursor in Actions like Insert Pair by Typing. Saves the extra action below.

2 Likes

That is a very cool tip. I just tried it, and it works very fast.
TextExpander had a similar feature, but it was very slow.

I've seen this hundreds of time in the Token popup, but never thought about it:

1 Like

Great macro. Now if only there was a macro that automatically add closing HTML tags. Maybe some day I’ll have time to create one.

What editor are you using, that doesn't support automatically adding closing HTML tags?

1 Like

I actually find myself typing HTML/PHP outside of dedicated editors more often than within them. I run and contribute to a number of WordPress-powered blogs, and I write HTML in the posts most of the time. Even more often than that I’m writing books or tutorial articles in Word or InCopy and adding a few lines of HTML for examples. Even in BBEdit matching tags don’t automatically appear.

I miss using Nick Bradbury’s TopStye Pro from my Windows days. That was a great CSS, HTML, and PHP editor.

I recommend Atom. Free, and the community of package developers is thriving. I regularly edit in Atom, then copy/paste into other places (including KM).

After installing Atom, there’s one package you need to install, and some others I recommend:

  • autoclose-html

I also recommend, for HTML development:

  • atom-beautify
  • atom-html-preview

I also like, for HTML development:

  • linter
  • linter-htmlhint

I’ll leave it at this for now. If you install Atom and decide you like it, I have a list of packages I really like, so go ahead and ask.

BTW, although you can remap keystrokes in Atom, it can be problematic sometimes, so when I need to remap Atom keys, I use KM.

1 Like

Hey Pariah,

Look for the menu item “Close Current Tag”.

Give it a keyboard shortcut in the prefs, and go to town.

Want something fancier? AppleScript can to magical things...

-Chris

Oh, yeah.

This can be done by Keyboard Maestro in any text field or window that supports cursor navigation – albeit in a clunky manner.

...
<body>
    Some body text
| <-- cursor

Of course much customization is possible...

-Chris

P.S. Pariah – you may also be interested in this:

http://www.angelwatt.com/coding/zen-coding_bbedit.php


Close HTML tag in Plain-Text Field.kmmacros (4.4 KB)

1 Like

I'm REALLY new to writing macros with KM (and Regular Expressions still faze me) but this one solves an annoyance I've had for a long time. Is there a way to include single and double quotes? I THINK I've modified the macro according to the instructions in the comment but when I try using the quotes, the macro, types the ; and the " then deletes them both and stops. Is there an escape character or something I need to do to use quotes in this? Thanks for any guidance you can provide. You guys make this old guy wish he'd started using a Mac a long time ago.

I'll take a stab at that (well, twice). This revision handles quotes at the beginning of a line, too:

Match Quotes Macro (v9.2)

Match Quotes.kmmacros (4.8 KB)

The trick is to avoid the apostrophe by trapping a leading space. And to put the cursor between the two quotes so you can just keep typing.

You can extend the quotes (to other quote marks) just by adding them to the trigger.

Oh, and if you want to toggle the macro on or off, this should do it (Option-'):

Toggle Match Quotes Macro (v9.2)

Toggle Match Quotes.kmmacros (5.9 KB)

Well, here's another variant that handles quotes with different close quotes (inspired by @JMichaelTX's bracket matcher above):

Match Quotes Macro (v9.2)

Match Quotes.kmmacros (7.3 KB)

1 Like

@mrpasini Alas, it doesn't work when you use the U.S. International PC keyboard layout, which uses dead keys for " and '.

@JMichaelTX Nice macro. With my U.S. International PC keyboard layout I only have to type the semicolon before the <. All other brackets don't require this trigger.

Would it be possible to use Enter as a way to position the text cursor behind the closing bracket? Kind of temporarily redefining this key?

So when you type apostrophe and space you don't trigger the macro? Try adding a space to the end of the regexes.

WOW...I certainly didn't expect a response so quickly. Thanks to all. I'll give this a go.

Just had a chance to try this; this is exactly what I was looking for. Thanks to all!