Regular expressions for converting code-indentation spaces to tabs in TextPad
I’ve never been a fan of using tabs to indent my code; I prefer spaces. Writing code is an art form, and when you use tabs to indent, you can’t assume that it will still look pretty on someone else’s machine, or in another application, etc., since tab sizes are platform-dependent and–although usually user-definable–the default size (typically 8 spaces, I think) tends to be much larger than my own 2-4 space indentation style.
However, some of my co-workers indent with tabs, and others indent with spaces. I’ve found that it’s easier for me to avoid inadvertently messing up existing code when I just bite the bullet and use tabs.
So, what follows are a couple of search/replace regular expressions I’ve recently used in TextPad, to make some existing code more consistent, by converting the spaces to tabs in certain relevant locations.
1. Regular expressions for aligning inline comments with tabs
| Find what: | \;( *) {2}(\t*)\/\/ |
|
| Replace with: | \;\1\t\2// |
|
Customization: The number 2, in curly braces (above), should be replaced with the number of spaces that are used for indentation, in the code you’re running this on. In this case, the code was indented in increments of 2 spaces. Sometimes I deal with code that’s indented in increments of 4 spaces, in which case that 2 would change to a 4.
Manner of execution: Run via Replace All, repeatedly, until there are no more matches.
Recommended scope: I used it on a very specific block of selected code. Keep reading for the specific format.
When I used this on a block of code that consisted of variable declarations and //-style comments, it ended up making the comment blocks all line up nicely. Here is an animated GIF1 of a series of screenshots, showing how repeatedly running this expression transformed the comments into tab-indented comments that lined up nicely:

I’m not sure how useful this will be, in general, because I think I just kind of lucked out when I got the results that I did. I thought it was worth sharing, though, because it impressed me when it produced results that were better than I had actually envisioned. :)
Two important things to note are:
- The actual variable declarations (beginning of each line) were already indented with 1 tab.
- There were a consistent number of spaces between the semicolons ending the variable declarations, and the double slashes starting the comments. While this didn’t help them line up, as spaces, it did make things pretty when the spaces were replaced with tabs.
If you’d like, you can see what my TextPad Replace dialog looked like, so you can see things like which checkboxes are checked, etc. Also, please note that in my TextPad preferences, I have it set to Use POSIX regular expression syntax (as previously mentioned, in another TextPad search/replace expression entry, a couple years ago).
2. Regular expressions for changing leading indentation spaces to tabs
| Find what: | ^(\t*) {4} |
|
| Replace with: | \1\t |
|
Customization: As with the first expression, above, the number in curly braces should be changed to the spacing increment used for indentation, in the code you’re running this on.
Manner of execution: Run via Replace All, repeatedly, until there are no more matches.
Recommended scope: It can be used on a block of selected text, the entire active document, or even on all open documents, if you’re brave and somewhat evil. :)
Footnotes
1 Sorry for using an animated GIF. I’d rather not have something constantly flashing on the page, because it distracts, and resembles an advertisement. I would have preferred another solution, but I racked my brain for over two hours, trying to decide how to present this series of 9 screenshots. I didn’t want to alienate readers who might read this using a feed reader that doesn’t support javascript, and I hate making people click through from the feed to the main site–I strongly prefer full-text rss feeds! Laying the 9 images out horizontally or vertically took up either too much space, or required making the images too tiny to read. In the end, this animated GIF seemed like the best portable way to show the effect I was trying to show. I won’t make a habit of using them, though. :)

