In case you were unaware, I’m a huge fan of TextMate for many reasons, one of which I’ll get into here — in case you couldnt’ tell by the title. Of course, the reason/tip is using regex in your search and replace.

To start this off, I haven’t read the TextMate: Power Editing for the Mac book and this tip is probably covered quite well in it. Regardless, let’s get on with the tip shall we.

When you’re using the search and replace dialog to make changes to a file there is an option you can check so the search can/will use a regular expression. This shouldn’t be new to anyone that’s been using TextMate for more than 10 minutes but every good tip needs a set up.

So, we’ve got the search and replace dialog box open now. In the search input you can place the regular expression you want to search for and in the replace box you can set what it should be changed to. Pretty straightforward right? Well, lets say you want to change the enclosure or tags around repeating bits of text — or even change a series of paragraphs into list items — and leave the text itself untouched.

With TextMate this is easy. Simply set up your regular expression in the search box. For example, if we were changing <p></p>’s into <li></li>’s the search string would look something like this:

<p>(.*)</p>.

This will look for all opening paragraph tags, any sequence or number of characters, followed by a closing paragraph tag.

Now we need to set up the replace statement. Obviously the text for each item will be different so we can’t include that in the replace statement. What we can do is use the automatically created variable from the search string. For this example our replace statement would look like this:

<li>$1</li>

The $1 is the variable that holds the text that was found by the (.*) in the search string. Putting the $1 variable in the replace string will place the text into the replaced statement in your file.

As I mentioned before, the $n variables are automatically created and work exactly how they do in .htaccess files. Let’s say you have a search string like this, <([a-z]{2})>(.*)</([a-z]{2})>. There would be three variables you could use in your replace string, $1 would be the two characters between the < and > symbols, $2 would be the characters/numbers/symbols in the middle, and $3 would be the two characters at the end of the string between the / and >.

So, anyone wanting to speed up their workflow and utilize the power behind TextMate’s search and replace better brush up on your regex knowledge, I know I am.

6 Responses to “Regex Search and Replace in TextMate”

  1. Brandon Burkett Says:

    Do you know how you would search/replace an html block like a unordered list? (.*) does not seem to work (because of the newlines after the ul tag.

    It seems that if a newline the first thing after an element, the regex has trouble finding it. With textmate is their regex like (.*) that includes newlines?

  2. Brandon Burkett Says:

    I figured it out. For anyone that wants to search/replace any character and newlines use ([.\s\S]*) between your tags.

  3. Mike Says:

    Another way to search for new lines is simply using \n. So for your list example it could be <ul>\n<li>. Of course, with this example you may run into issues with tabs (\t) if you follow any type of indentation in your code.

    Glad you found a way to do it though, and thanks for following up with a comment here.

  4. Andy Miller Says:

    Ooh I actually understood this. Thanks

  5. Mike Says:

    Glad I could help in some way Andy. :)

  6. Wolf Says:

    Sweet tip, one step closer to Textmate heaven.

Leave a Reply