Using TextMate for WordPress Code Cleanup

I spend a lot of time cleaning up WordPress themes. During the code cleanup I often perform certain cleanup tasks over and over, which makes them perfect for TextMate commands.

In this post I’ll show you how to add two useful commands to TextMate, then move through the steps I take for theme code cleanup and put the commands into practice.

First, let’s add the commands to a TextMate bundle. If you don’t know how to add commands to a TextMate bundle, or don’t have your own bundle set up yet, start here and add a new bundle. I usually add my own commands to a bundle called @lance so it sticks to the top of my bundle list.

“Remove Trailing Spaces in Document / Selection” Command

Select your bundle and use the “New” menu at the bottom left of the bundle editor to add a new command. Name it “Remove Trailing Spaces in Document / Selection” and paste the following code into the “Command(s)” field.

perl -pe 's/[t ]+$//g'

If you want to map the command to a keyboard shortcut, find the “Activation” menu, click the input area, and enter the keyboard shortcut. I use Cmd-Shift-0 for this command. Leave the other settings as they are, and save the command.

Here’s what the bundle editor window should look like for this command:

Credit: Albert Davidson Chou.

“Change Spaces to Tabs” Command

Follow the same steps as above, but use the following code instead in the “Command(s)” field, and change the command title to “Change Spaces to Tabs.”

$fetch = fread(STDIN, 100);
if (! strlen($fetch)) {

# set this to what you want
$tablength = 2;

$data = $fetch;

while (strlen($fetch)) {
	$fetch = fread(STDIN, 1000);
	$data .= $fetch;

$output = '';
foreach(explode("n", $data) as $line) {
	$len = floor(strspn($line, " ") / $tablength);
	$output .= str_repeat("t", $len) . substr($line, $tablength * $len) . "n";
echo substr($output, 0, -1);

I use Cmd-Shift-9 as the keyboard shortcut for this command.

Credit: M Spreij.

Tab Size Setting

Next, check your settings for Tab Size at the bottom of your TextMate document window. I use “Tabs: 4” with “Soft Tabs” unchecked.

Now, let’s use the newly added commands.

  1. Remove extra line endings and check for extra spaces or newlines after PHP blocks.

    Run the “Remove Trailing Spaces in Document / Selection” command on each file. If you don’t have any text selected, the entire file will be changed.

  2. Convert spaces to tabs at beginning of lines.

    Run the “Change Spaces to Tabs” on each file.

  3. Follow WordPress conventions for spacing around braces, function calls, and variables.

    See WordPress Coding Standards under “Space Usage” for details.

    Use TextMate project search/replace (CMD-Shift-F):

    Find (' and change to ( '
    Find ') and change to ' )
    Find if( and change to if (

    Note: This won’t catch all the cases. The remaining PHP code will need to be cleaned up by hand if you want to be thorough about spacing.

  4. Add missing ending semicolons.

    Use TextMate project search/replace to find ) ?> and change to ); ?> and to find )?> and change to ); ?>.

  5. Use single quotes in PHP.

    You should always use single quotes in PHP unless you have to include variables, or wrap strings with single quotes in them. Find "" and change to ''. For example, <?php single_cat_title( "", false ) ?> needs to be changed to <?php single_cat_title( '', false ); ?>.

  6. Spell WordPress correctly.

    The most common mistake is to use a lowercase “p”. Use TextMate project search/replace for Wordpress and change to WordPress.

Why do all this cleanup?

If you work alone, these changes might seem petty or nit-picky to you. As soon as you collaborate with another developer, however, having consistent coding standards makes sharing code much easier.

Looking at a diff of each others’ code will be more useful if the differences match only the changes that really matter. If each change has whitespace, indentation, and coding style differences, it will be much harder to find the changes you care about.

On a higher level, code you contribute to core WordPress or the community as a whole in the form of a plugin or theme should conform as much as possible to WordPress Coding Standards. Using these few TextMate commands to quickly clean up your code will go a long way towards meeting those standards.


Author: Lance Willett

Lance is a blogger and web craftsman making high-quality, engaging, and user-centered experiences for WordPress. México-born. Product Excellence Manager at Automattic.

11 thoughts on “Using TextMate for WordPress Code Cleanup”

  1. These are awesome time savers. I spend a lot of needless time doing the same things you’ve listed here. You probably just saved me a full week of time in the next year. Now I just need to figure out what to do with all that extra time 😉

    1. Haha—that’s a good problem to have, Brandon. Let me think… oh, you could speak at more WordCamps and spread goodwill to people everywhere with your theme-fu.

  2. We actually just switched to soft-tabs set at two spaces. It helps preserve formatting regardless of the editor you use. That said, consistency is key to having maintainable code.

    Thanks especially for the the Textmate tip 😉

  3. I love this editor:

    Handles removing trailing whitespace on save and adding an empty newline in the bottom automaticly among a lot of things. Yet it’s very clean and all of these things need to be enabled so they won’t confuse a person trying it out. 🙂

Comments are closed.