Skip to content
By Chris Herring

Stripping Whitespace out of Textmate 2

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 plauged 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 doens'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 wtih 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.

Latest Articles by Our Team

Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.

We Hire Only the Best

reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.

We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.

Free Community Workshops

We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.

  • Webinars

    Webinars

    Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.

    Learn more about webinars

  • Installfest

    Installfest

    The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.

    Learn more about Installfest

  • Development Hub

    Development Hub

    The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.

    Learn more about Development Hub

Get the “reinteractive Review” Monthly Email