Sorry, Peter. I guess I misunderstood and mischaracterized your macros.
No worries, I tend to err too much on the “vague description, now you will learn something as you build it” side of “here’s a finished macro which you can use without thinking”. But hopefully there is enough there to understand the concept now, it’s a pretty neat solution I think which could be used in a number of different classes of problems.
What happens if you type one bracket, say "[", and then type "{", BEFORE you press the hot key?
Don't you end up with an unbalanced "[" ?
Since you've already triggered one macro with typed text, why not just complete the job in that macro, moving the cursor back just inside of the closing bracket?
I believe the OP does not want the facility to be automatic, but wants it to happen only after a hot key is pressed after the bracket is typed.
You could just have macros like:
Typed String trigger (
Action: Type ), Type Left Arrow
to always balance the brackets.
But the OP wants to be able to press a hot key later on to have the bracket added.
So yes, if you type [ and then { and then press the hot key, you would get } and then if you pressed it again, you would get } again.
You could extend the concept to keep a stack of outstanding brackets to have it work in that sort of case - it wouldn’t even be particularly hard, but probably not worth doing unless it is a use case that is expected.
Thank you so much @peternlewis, this is perfection. KM remembering triggers is actually really interesting and quite useful. Do you or anyone else use this kind of mechanism to do any other useful macros outside of this one?
Thank you once again, will be using this little macro quite often. I still need to get my head around how to actually make and combine multiple macros to make more useful things, hope this kickstarts some ideas and things in my brain.
This will probably be my most used and most useful macro, thank you @peternlewis for showing me how you can do this. I just want to make it a little bit more robust. Right now I have this :
And this works wonderfully if I type my trigger and press my character trigger and press my hotkey straight away. I can press left bracket, my hotkey trigger and I am in-between the brackets and thus I have saved me some time. There can also be a case where I opened my bracket, typed some words and letter characters and want to close my bracket now that I forgot but now if I press the trigger, it closes the brackets but also moves pointer to the left every time.
Would it be possible to extend this macro to accompany these cases? Essentially, if character string is typed and trigger is pressed right away, it will do one action. If character string is pressed and some other letters are typed that are also not any of these characters :
It will activate another action, in my cases it would just be closing the bracket and not moving the pointer. I would think I would need to perhaps use some regex for this but I both not really sure where to apply this regex and what regex would that be.
A regex “.” trigger will match on most every character (except returns and non-matching keys like arrows).
The triggered macro could record the character and then you could compare it to Last Bracket and if they are different some other character has been typed.
It’s not entirely ideal since you’ll be triggering on every character you type, but in practice its probably fine if it is limited to when you are in a typing-style application - you generally don’t need performance in such applications.
To all who may be reading this:
(I'm sure Peter knows all this)
I can't say that I'd recommend using RegEx ".
" as a trigger.
Not only does it have some, maybe substantial, performance hit, but it also:
- Will likely lead to an issue someday, causing something else to not work, and you won't have a clue as to why.
- Will fill up your KM log files
- Is a big price to pay for such a very, very small convenience.
The biggest problem of all software is unintended consequences.
Try not to shoot yourself in the foot.
Sorry for the side note, Peter.
An alternate solution might be to note the time of the first macro firing, and then behave differently if more than a small amount of time has passed when the second macro fires.
First macro:
Set Last Bracket Time to calculation NOW()
Second macro:
If calculation NOW() - Last Bracket Time > 2 …
Sorry, Peter, but I couldn't help myself at the humor here:
So much work for so little gain. Not something that you usually advocate.
Yeah I think having a regex for all characters is a little too much for my use case
I may try implement the alternate solution though and see if that works well, I think it can be really useful. Alternatively I can make a huge switch case on every character and symbol and see how that goes. I think it can be pretty cool.
Nevertheless, thank you all for the help. This is seriously awesome. One more step to having system wide vim available in all apps.
I have had a different approach to this that I have used for some time, which is very simple and I apply it to a range of punctuation and it quickly became part of my muscle memory.
Anytime I hit ‘(’ I immediately hit ‘)’ and then have KM move my cursor back in between the two brackets. This is good because it also means that I keep my finger on the shift key while pressing ( ) in quick succession.
I use the same approach for all the following pairs:
( )
[ ]
{ }
’ '
" "
_ _
Yeah, the thing is that I don’t use shift key for putting in symbols, I am holding i key. That’s why a universal hotkey is so needed for me. But this works perfect right now.
I’ve adopted the same strategy. Although in TextMate this is automatic. Along surrounding selected text in the typed character. So I can get confused in going to other app and back.
Thanks, @Rather.
A useful approach.
But this question:
Why type close bracket at all, when KM can do that.
Whenever you type open-any-bracket, KM would type close-that-bracket, and then one backspace to put cursor in the middle.
I can imagine one Switch/Case action plus enough triggers for every sort of bracket you use.
Seems a bit more efficient, but I haven't actually done it.
Would that work, or not?
I am trying to extend this to match two consecutive symbols distinguishing between one symbol and two in the stack.
For example I have a text trigger for < symbol :
But I also want to have a text trigger for <! combo like so :
I then can set the trigger to open up for what I want :
I thought typing : <!
and activating my trigger would expand the above thing but no it doesn't. It expands my < trigger. Is there a way I can keep account of previous symbols?
Thank you for any help.
I am late to this discussion. Not sure what I am missing here but … why not just put in a macro for each instance? Seems that if this is something you are doing a lot of, why not make it as easy as setting an auto-expansion for your signature, today’s date, etc.?
I use the semicolon key to trigger my macros in Typinator all the time. You could use any symbol or letter you wanted to, just make it a unique combination that you would normally type only for that purpose.
For example to get this “(I)” instead of trying to get it to auto-expand, I would think about setting a macro to a specific key trigger - one I would repeat in a duplicate macro for my other needed brackets or surround symbols, I would set up a new macro like so:
“;(” which would type “( )” & “key left” yields; “(I)”
";[" which would type “[ ]” & “key left” yields; “[I]”
";{" which would type “{ }” & “key left” yields; “{I}” (with uppercase obviously added to get the bracket key)
I too use typinator a lot and use : as my trigger key. The way I have configured my keyboard, I need this one universal hotkey to cover all of my cases because typing my trigger key : then what I want to expand then pressing space takes too long of a time. What I have currently works flawlessly, I just need to know if I can override single symbol remembering like < and ! when <! are typed together so both of these symbols go into my variable and not the last one typed.
[quote=“nikivi, post:38, topic:5575, full:true”]… what I want to expand then pressing space takes too long of a time.[/quote] If that is not going to work for you then, OK. But to be clear it does not take three key hits to do this. You don’t have to hit the space bar. Just (in your case) hit the colon key (semicolon is easier as I don’t have to hit shift to get to it) and the left paren key or whatever your expansion will be. Because I use a specific trigger value that I am very unlikely to hit accidentally I set my macros to not need the space bar after typing to trigger them. So, its one two (boom - expansion) and not, one, two, three. Might be a small difference but as a musician, it’s all about rhythm and two hits on the keys is easier to remember for my brain than is three.
@Mark asked:
But this question:
Why type close bracket at all, when KM can do that.
Once my left index finger is on the Shift key, my right index and middle finger can hit the bracket pairs within about one-tenth of a second.
Even for pairs such as * * or " " two stabs at the same key take about the same time.
The later example by @levelbest
";(" which would type "( )" & "key left" yields; "(I)"
would take longer because there are four separate keys, spread widely across the keyboard, that have to be hit. My approach uses three keys; one which remains depressed and the others which are neighbours.
(Plus, I've been doing this so long that I would have to retrain my muscle-memory, which would lose more time than I gain! )