Match Symbols, Brackets with one hotkey

Often you have to type in brackets like these : () or [] or {}.

I really wish I can create a hotkey that if I type in just the left bracket : ‘(’, I could just press a hotkey that it will instantly autocomplete and match my bracket. This would save me a lot lot of time.

My current thinking how I can do this is maybe okay but I wish I can do it faster. It will go back one place and highlight (shift + left), copy it to buffer, move right, check what was copied and have a switch case to insert the matching bracket.

Alternatively, I think km should do some kind of logging of what characters were typed to do its text expansions, can I make use of it somehow to instantly expand the bracket or " or ’ with one hotkey to cover all cases. I just need to know the last character I typed and check on that.

@nikivi -

  1. Your description above is confusing to me, but it seems interesting.
    Please make it more clear.
    Some examples would help.
    Step-by-step actions would also help.

  2. Even though I don't understand the problem yet, what comes to mind is the way this forum works:
    Highlight some text, then click on Quote, Bold, or Italic, etc, and the text is bracketed with markdown symbols.
    Would that do what you want, or not?


  _I want this to be italic._

I haven't done that with KM, but seems easy.
What have you tried so far?
I'm trying to understand why is this a difficult problem.
Please explain with more details.

Yeah of course. Hope this will clear the confusion.

If I am here :

Pressing the hotkey will do this :

If I am here :

Pressing the hotkey will do this :

And so on, for { and ' and ".

That helps, thank you.

So why not just assign each LEFT bracket as a hot key trigger?

You’ve already described matching it with the RIGHT bracket, and then reversing one space to type in the text.

Or use Switch/Case for each style of bracket, plus a little RegEx maybe.
(I think, but I haven’t tried it.)
It seems too easy, so I must still be missing something.

Have you tried any macro action yet?
Can you post the macro image here?

I already do something like this with “(”, so I am interested in your approach.
Maybe you have a better idea about this.

Reason I can’t just match left bracket as a tigger is because I don’t always want matching brackets or matching symbols. Ideally as I mentioned above, I want to know what was the last character I typed was and check for it and expand according to the character and bind it to one hotkey only. Perhaps @peternlewis knows if one can do this. If you can that would make my day.

If not, then I don’t see any other nice way to solve it, and I am resorted to moving the pointer, checking and inserting then which sacrifices speed. :frowning:

A couple of other ideas:

  1. Have the macro do a . This should select the previous character. Then do a Copy, followed by . Then use a Switch action to look at what the character is, and add the proper closing delimiter.
  2. Alternative, make macros that are active only for that application, and use a typed string trigger of, for instance, “(”. Automatically translate this to “( )” followed by . Of course, that means you get the matching character even when you don’t want it, but since lots of editors do this already, it’s not a complete unfeasible solution.
1 Like

Here is the macro I am using and it works :

I still wish though I could directly get the symbol from the clipboard stack or something if KM uses that in any way. Right now I really dislike the delay I have to wait before I can type again.

Looks good! I’d change those “IF” actions to a single “Switch” action, because it’s cleaner. But it isn’t necessary, of course.

I haven't test this, but I would probably do something like this:

  • Trigger: Typed Text of ;[
  • Set KM Var "Symbol" to 2nd char in trigger
  • Use Switch Action to set KM Var "SymbolClose" to matching symbol
  • Insert text by pasting %Symbol%%SymbolClose%
  • type keystroke of LEFT_ARROW

Yes, I thought about making a text expansion with a text trigger but that doesn’t work for me. I much prefer a hotkey. The only question is whether I can access the last typed character directly and not do the copying step.

I would assume that KM keeps a track of certain number of characters in some register for its text expansions, can we access this register in any way?

Not that I know of, but it would be an interesting feature. @peternlewis - your thoughts?

Make a macro Remember Bracket

Trigger by Typed String "(", Typed String "[", Typed String "{" (you could use a regular expression, but three Typed String triggers is probably easier than figuring out how many backslashes you need to make a regular expression for those three characters!). Each one should be configured to not delete the characters.
Action: Set Variable Last Bracket to %TriggerValue%

Macro Balance Bracket
Trigger: Hot Key whatever
Actions:

  • Switch on variable Last Bracket
    • "(": Type Keystroke ")" Type Keystroke Left Arrow
    • "[": Type Keystroke "]" Type Keystroke Left Arrow

It's technically possible, but perhaps a bad idea for security reasons. In any event, it is unnecessary for this case.

1 Like

That's clever, Peter. Maybe @nikivi will like your text expansion macro better than mine. :wink:

I forgot we can turn off the typed character deletion.

I am a bit confused about the variable and the macro itself nor do I see where my hotkey is being used. Can you help me clear up my confusing a little. This is what I got by trying to understand what you mean :

No one knows how to solve this? :frowning:

Er a, well a, if you would be willing to consider using a text expansion macro, you actually have at least two good choices. Why don't you like the text expansion method?

Because for me a hotkey is single press and text expansion requires few more presses and a little time lost. If I understood @peternlewis correctly then he essentially solved my problem of trying to get around the copying part and using a character based trigger that can then be completed with a hotkey to match the bracket last entered. However I just don’t know how to implement it sadly.

I would approach it this way:

Highlight the text you want bracketed;
Press hotkey "("
Macro performs these steps:

  1. Cut to Clipboard
  2. Type (
  3. Paste from Clipboard
  4. Type ) + space

Then I would make a macro that would be triggered by control+( which would type a normal single (.

Then I would build instances of these for the other two kinds of symbols.

Not as fancy or efficient as the others, but it satisfies your requirements -- single press hotkey, but still have a way of typing a single left bracket.

HTH

I'm not sure that is correct. You may want to try it just to see how it feels.
To use a hot key requires TWO keystrokes: the SYMBOL and the HOT KEY, like:
[⌘E

@peternlewis' macro uses the SYMBOL as the typed text trigger, so all you have to type is ONE character:
[

My macro uses the semicolon (";") as a prefix to prevent unintended trigger.
But, it is still only TWO keystrokes: SEMI-COLON and SYMBOL:
;[

So what's the big advantage, or time saver, of using a hot key?

I tend to sleep at night, I work better that way.

As I described, there are two macros.

The first one is called "Remember Bracket" and has your four triggers, and all it has is one action: Set Variable "Last Bracket" to "%TriggerValue%". It's purpose is simply to remember the last bracket style you typed and nothing else.

The second macro is called "Balance Bracket" and has your hot key trigger. It has one compound action, a Switch/Case action which switches on the variable "Last Bracket" and does the appropriate thing, something like:

1 Like