TextMate News

Anything vaguely related to TextMate and macOS.

Chaining Snippets

It is sometimes useful to insert a list where number of items is unknown, but each item has enough boilerplate to want a snippet for it.

One approach to give this a more natural flow is chaining of snippets done e.g. for Rails migrations.

A thread on the mailing list recently resulted in this tip from Brett Terpstra

The idea is that the first snippet inserts the full list and has the last tab stop after the list end marker. So that would be (the class name here is generated from the entry text, specifically it is lowercased and spaces are replaced with underscores):

<ul id="${1:navbar}">
    <li class="${2/( )|\w+/(?1:_:\L$0)/g}">
        <a href="${3:#}">$2</a>
    </li>
</ul>$0

Another snippet inserts a new entry and the list end marker. It has the list end marker itself as tab trigger (i.e. </ul>), so the snippet is:

    <li class="${1/( )|\w+/(?1:_:\L$0)/g}">
        <a href="${2:#}">$1</a>
    </li>
</ul>$0

This means that when the caret is located after </ul> you can press tab, and you will extend the list with a new entry, and be able to fill in the text for this new entry.

This means initial creation of the list is just an extra tab for each additional item, and at a later time, it is possible to add new items to the list by placing the caret after the li end tag and hit tab.

categories TextMate Tricks

4 Comments

hey that’s a very nive idea !

I believe the ‘}’ character in the first line of the first example snippet should be omitted.

branden: fixed, thanks!

Yeah, I came to comment on that after I saw it in my RSS reader. I had originally had a tab stop around the whole id but removed it to simplify the creation of the navmenu (and apparently left a little behind). Looks like I pasted before I should have…