Blog

Stripping Whitespace out of Textmate 2

Placeholder Avatar
Chris Herring
December 19, 2011

So as you may know, the long awaited arrival of TextMate 2 is upon, albeit in alpha form. Being the excitable fellow that I can be, I of course jumped on this the absolute moment that I could, closed down my MacVim instance and downloaded TextMate 2. I started it up, and oh no, again I found myself confronted with trailing whitespace that had plagued me all through my TextMate usage. But some talking to other folks I discovered that all was not lost, and I had indeed had the power to rid myself of this, as I had done in the past without knowing it.

In the text bundle there is a command, Remove Trailing Spaces in Document / Selection. remove whitespace screenshot Excellent, I wish I had known about this sooner, but now how do I get this run everytime that I save my code. There was some talk of macros, and this may be the way really to get this done, but I needed to find out how to make a Bundle, as TextMate 2 seems to be currently taking away bundle modifications on update during the Alpha, except for ones that are your own. Not wanting to have to learn how to do two things at once, this is the process I used, and it works!

First you want to get to your Bundle editor, very easy, clicking Bundles in the Menu, then on Edit Bundles, or for those of us Keyboard inclined, Control+Option+Command B. You will then be presented with a screen like so.

bundle editor

Excellent, now we need to create a new Bundle. There is no menu item for this that I could see, but our trusty Command+N comes to the rescue here, and we are presented with some options. new bundle

Nicely the first thing that shows in the dropdown is Bundle, and this is what we want, so click create and off we go. Once we have done this we are presented with our Shiny New Bundle, for me this is called Chris’s Bundle.

shiny bundle Now we want to create a command for this bundle, and it should go inside the Menu actions, so click on menu actions, and then again press Command+N. Select Command from the dropdown list, and click create.

new command Now to the right of your bundle editor a configuration screen should slide out, and we use this setup our command. Now my preference is that all whitespace should be stripped everytime I save any document. This works well for me as I only use TextMate to write code, so I have my command configured like so, command configuration The quick rundown of this is, I have named it Save and Strip Whitespace, I want it to trigger on Command+S, when it runs it should take the current document as input, and replace it with the result of the command. By choosing Line Interpolation as the caret placement my cursor stays on the current line after running the command. Long story short after my command is run there is no more trailing white space, and my cursor stays put. I discovered that @ is the symbol here to bind to command by looking through existing items and then comparing with bundle commands that already exist, so if you wanted to bind to something else, go investigate the keys quickly. Now we are not quite done, we have set up to run a command, but it doesn’t have anything to execute yet, and as I didn’t want to fiddle with Macros, I looked up the current TextMate command to strip White space, and I copied it into my command, the spot to do this is the Text input area under the bundle editor list.

command text input If you want to copy and paste it, here it is: #/usr/bin/env bash [[ -f “${TM_SUPPORT_PATH}/lib/bash_init.sh”]] && . “${TM_SUPPORT_PATH}/lib/bash_init.sh”

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

Of course if you have a preferred command in another language or something please put it in here, and maybe share it with us.

Now you just need to close your bundle editor, it will prompt you to save your unsaved item, click save and then you are good to go. Free to code without big red blocks in your git diffs!

Enjoy!