Posts RSS Comments RSS 32 Posts and 1,017 Comments till now

TextMate Tip – Macros

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:

<html>
    <li class="lstitem">
        lorem…
    </il>
    <li class="lstitem">
        lorem…
    </il>
    <li class="lstitem">
        lorem…
    </il></html>

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)
  • Press ⌃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 (⌥⌘M again)
  • 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”

  1. on 05 Feb 2008 at 7:09 amVid Luther

    Ciaran, I’d be interested in finding out your opinion on this, whether what I want should be a bundle, or a set of macros. Here’s the scenario.

    I have *.asc files set to open with TM, these .asc files contain GPG/PGP data, usually keys, and sometimes signed messages. I’d like to be able to have TM verify the signature of the text I’m reading, or import the key into my PGP keyring. On a daily basis I get maybe 20-30 messages with attachments that I need to verify signatures for, I can do all of this in the cmdline, but it would be nice to have a GUI, I’d like to be able to eventually manage my keyrings through a GUI. There are things like engimail, and gpgmail, but they aren’t exactly what I need. I could use some direction in how you think this should be handled.. we can talk more on irc :) .

  2. on 14 May 2009 at 4:39 pmjohnny

    is there a way to repeat the macro till the end of document? i used use textpad which could do this. very helpful to quickly change a list of items

Fork me on GitHub