Replacing TextExpander with Keyboard Maestro

· geek ·

If you use TextExpander at all, or follow people who do on or Twitter, you will have noted that there was something of a kerfuffle after Smile (who make TextExpander) announced that they were changing to a subscription model (and thereby increasing the price of using the software substantially). I won’t go into all the arguments (you can read a round up of various opinions collected by Michael Tsai here). Smile have since altered their position, providing a much reduced lifetime subscription cost for current TextExpander customers, and promising to maintain the current version 5 for those who preferred its features to those of the new version. I’m not a great fan of subscriptions, except for more conventional services (like Dropbox and so on) where it makes more sense, so by this point, I had already looked around for an alternative and switched. I was reminded that I already own and run plenty of applications that provide snippet expansion as one of their features: LaunchBar, Alfred, Dash, Keyboard Maestro, and it is even built into the operating system in El Capitan. After a bit of thinking, I went with Keyboard Maestro and have been very happy with the switch.

I have used TextExpander since it was developed by Peter Maurer and called Textpander. I found it very useful and convenient, and just kept upgrading each time a new version came out. I think that’s why I hadn’t thought seriously about whether I could get the same utility from other applications I had installed. In general, if two applications have overlapping features, I would prefer to run one rather than two, so it makes a lot of sense to simplify a bit.

What I hadn’t expected was that I have been missing out on a lot of very handy features by not using Keyboard Maestro to expand snippets as well as do all the other automation on my system. However, if you only wanted to expand snippets, Keyboard Maestro would be overkill, and other dedicated applications like TypeIt4Me (which I used very happily for many years) or Typinator would probably serve you better. Keyboard Maestro has no end of power and possibility, but setting up your snippets initially is a little more work.

I took the opportunity to cull my snippets and rationalise them a bit. As with any collection that has built up over time, it was a bit ramshackle, and some of the snippets were not really needed any more. Keyboard Maestro allows you to use all kinds of triggers to execute its macros, one of which is a typed trigger, which is what you want for snippet expansion. For basic snippets, all you need to do is enter your abbreviation as a trigger (I prefixed all mine with a semicolon this time for consistency), and then add an action to either insert text by typing or by pasting (the former is better for web forms, the latter better for larger amounts of text, and is faster). Furthermore, you can create groups of snippets that will only be available in certain applications, or will not be available in certain applications (just like TextExpander). You can also insert tokens to insert the clipboard contents, dates or times formatted however you like, or insert the cursor at a certain point. So far so good.

The interesting part came when I thought about how to improve my group of Latin names snippets. If you’re a biologist, you often have to type Latin names, which are typically long and hard to spell correctly, and are therefore perfect candidates for a snippet. They should also always be in italics. TextExpander could handle the expansions fine, and you could set up a rich text replacement to put the text in italics, but this wouldn’t handle the situation when I write in Markdown or Org mode in a plain text editor, when italics is indicated by pairs of characters (underscores, asterisks, or forward slashes, depending on settings) wrapping the text.

I realised that I could make a macro that inserts the text, and checks whether it is running in one of a list of rich text applications (Word, Pages, etc.) or in Emacs. If it is the former group, it executes the keystrokes to select the words just pasted and then makes them italic with Cmd+I1. If the macro is executed in Emacs (actually, Spacemacs), it runs the Vim keystrokes to select the text (Esc v bb) and then make italic (Space m x i A). I can’t tell you how excited I was when this actually worked as expected in both kinds of application. I keep triggering Latin words now, just see the magic happen.

There was one final wrinkle I had to work out. Latin names are usually two words (e.g. Homo sapiens) but when you are talking about a sub-species, it is three words (e.g. Gorilla gorilla gorilla2). The macro needed to know how many words to select back from the insertion point to run correctly. I created a shell script using pbpaste | wc -w to count the words in the clipboard and save the number to a NumWords variable. The macro then loops the relevant keystrokes that number of times to select the correct words. Since you can call a macro from another macro, I made the ‘select words and make them italic’ bits of the macro into a standalone macro. This means that when I make Latin name snippets, all I need to do is add an action for the actual text to type, then include the selecting macro to do all the rest. Furthermore, if I want to tweak the selecting macro, I only need to do that in one place, and all the snippets will run the updated version.

You can do some pretty sophisticated things with Keyboard Maestro, and I’ve found that it works very seamlessly when inserting snippets.

  1. Usefully, this also works in Ulysses, a Markdown editor, even though the effect on the text is completely different. ↩︎

  2. Yes, that really is the correct Latin name for the Western Lowland gorilla — so good they named it three times. ↩︎