I’ve not posted for far too long as I’ve been busy with the holidays, starting a new job and working on a project, so apologies for the downtime, but hopefully this marks the return of regular posting. I’ll also try to work through all the comments as I’m a bit behind, so if I’ve not replied to you then apologies again.
Today I’m going to give some information about using Macros. Macros are simply a set of actions which you can record and then play back whenever you like. Most actions, including text editing, find and replace, clipboard usage, commands and snippets will be recorded, but there are some which are not (switching files and moving to a symbol for example).
Scratch macros can be very useful when you need to repeat the same set of actions a number of times. Let’s take a HTML example:
lorem… lorem… lorem… ⋮
We need to correct the class on each of the list items to be "listitem" and correct the closing tags to "</li>"; column typing won't work because there are lines in between the ones we want to edit, although using find and replace would be possible but would require 2 invocations. So, we can use a macro:
- Position the caret in the top class name, where the "i" needs to be inserted (i.e. at l‸stitem)
- Start recording the macro: Bundles → Macros → Start Recording (
⌥⌘M) – a pulsating red dot will show in the bottom-right of the editor window
- Insert the 'i'
- Move the caret down to the closing tag, between the "l" and the "i" (i.e. at l‸i)
⌃T(this performs a transpose: it swaps the characters on either side of the caret)
- Move the caret down to the next class name where the "i" needs to be inserted
- Stop recording the macro: Bundles → Macros → Stop Recording (
- Now you can repeat the macro as many times as required to make all the corrections by pressing
⇧⌘M(or the menu item: Bundles → Macros → Replay Last Recording)
This was a simple example but hopefully you can see how the same idea can be applied to more complex situations.
Macros can also be useful when you have a common set of tasks which you perform often, e.g. if you often have to perform a specific global replace, you can record it once and save it to your bundle using Bundles → Maros → Save Last Recording… (
⌃⌘M) – in fact many bundle items are macros, for example the Delete Line and Join Line With Next items in the Text bundle.
I’ve been having difficulty thinking of subjects for new tips, so unless I get a request I plan to make the next post a look at the HTML bundle.
2 Responses to “TextMate Tip – Macros”