A keyboard shortcut to replace soft tab spaces with hard tabs in TextMate

September 20th, 2010

Don’t you hate it when you realize you’ve been editing a file in soft-tabs mode and it’s turned into a jumble of tabs and spaces? TextMate includes a menu item to convert tabs to spaces, but it’s not accessible via an easy keyboard shortcut. Here’s a simple bundle command to replace the spaces at the beginning of each line with tabs, based on your current tab size setting.

perl -pe '$tabs=$ENV{"TM_TAB_SIZE"}; 1 while s/\G {$tabs}/\t/gc'

Hat tip to Greg Bacon1 for the perl one-liner2 that makes up the bulk of this command.

In the example above (click to enlarge), I have the command bound to ctrl-shift-tab.

To do the opposite, converting tabs to spaces, use:

perl -pe '$spaces= " " x $ENV{"TM_TAB_SIZE"}; 1 while s/\G\t/$spaces/gc'

tagged , , ,