Teaching TextMate a new trick

I recently had to switch to TextMate 2 because I had upgraded to OSX 10.10 Yosemite, and my trusty old version of TextMate was no longer fully operational. Some of the bundles I am routinely using when formatting eBooks crashed Ruby, making them useless.

One of the features I constantly use is the “Wrap Each Selected Line in Open/Close Tag” command from the HTML Bundle. It is one of the most important commands I use when formatting eBooks, because it allows me to automatically wrap every paragraph in a text in <p> and its corresponding </p> tags.

After the update to TextMate 2 I found, however, that the functionality was no longer what I really expected. Instead of wrapping everything in <p> tags, I found my text wrapped in <li> tags instead, which was not very useful. Actually, it was not useful at all. It was detrimental.

I’ve done a little bit of TextMate Bundle coding myself in the past to create streamlined commands that help me format eBooks much faster. They have become essential part of my everyday tool chain. Seeing the unexpected behavior of the TextMate 2 bundle I decided to simply dive into the Bundle Editor and change the default behavior of the “Wrap Each Selected Line in Open/Close Tag” command to my needs.

If you want to do the same, here’s how you’d go about it. Simply open the Bundle Editor by selecting “Edit Bundles” from the “Bundle” menu in TextMate. Next, select the HTML entry in the list that appears on the left hand side of the window. You will now see a number of entries appear in the second column of the window. We want to drill into the one called “Menu Actions” and then the one called “Wrap Each Selected Line in Open/Close Tag”.

droppedImage

In the lower window that contains the code portion of the command, you will find the following lines

#!/bin/bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
perl -pe 's/[\$`\\]/\\$&/g; s/([ \t]*)(.+)/$1<\${1:li}>$2<\/\${1\/\\s.*\/\/}>/'

As you can see, in the jumble of the regular expression there is a reference to the unwanted “li” tag. All we have to do is change that entry to “p” instead and make the line look like this

#!/bin/bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
perl -pe 's/[\$`\\]/\\$&/g; s/([ \t]*)(.+)/$1<\${1:p}>$2<\/\${1\/\\s.*\/\/}>/'

Once you’ve done that, hit Ctrl-S to save the change, and you’re done. Whenever you now press the Shift-Control-Command-W keys or select “Wrap Each Selected Line in Open/Close Tag” from the Bundle menu, every paragraph of your text will be neatly wrapped with the correct <p> tags.

Facebooktwitterredditpinterestlinkedinmail

2 Replies to “Teaching TextMate a new trick”

  1. Mark

    Uhhhh … why not just do this:

    ⇧ + ⌃ + W

    it wraps the selected text in the paragraph \ tag. Are you just trying to save a step?

    And yes, I don’t get why the \ tag is in that snippet.

  2. Guido

    That’s exactly what I’m doing but when I upgraded to TextMate 2 that functionality no longer worked properly. Hence the fix to set it right again.

Leave a Reply

Your email address will not be published.