index.md (2410B)
1 --- 2 title: "Out, Damned Tabs" 3 date: 2009-04-09T00:00:00+00:00 4 draft: false 5 canonical_url: https://www.viget.com/articles/out-damned-tabs/ 6 --- 7 8 Like many developers I know, I'm a little bit OCD about code formatting. 9 While there are about as many ideas of properly formatted code as there 10 are coders, I think we can all agree that code with tabs and trailing 11 whitespace is not it. Git has the `whitespace = fix` option, which does 12 a fine job removing trailing spaces before commits, but leaves the 13 spaces in the working copy, and doesn't manage tabs at all. 14 15 I figured there had to be a better way to automate this type of code 16 formatting, and with help from [Kevin McFadden's 17 post](http://conceptsahead.com/off-axis/proper-trimming-on-save-with-textmate), 18 I think I've found one, by configuring 19 [TextMate](http://macromates.com/) to strip off trailing whitespace and 20 replace tabs with spaces whenever a file is saved. Here's how to set it 21 up: 22 23 1. Open the Bundle Editor (Bundles \> Bundle Editor \> Show Bundle 24 Editor). 25 26 2. Create a new bundle using the "+" menu at the bottom of the page. 27 Call it something like "Whitespace." 28 29 3. With your new bundle selected, create a new command called "Save 30 Current File," and give it the following settings: 31 32 - Save: Current File 33 - Command(s): blank 34 - Input: None 35 - Output: Discard 36 37 4. Start recording a new macro (Bundles \> Macros \> Start Recording). 38 39 5. Strip out trailing whitespace (Bundles \> Text \> 40 Converting/Stripping \> Remove Trailing Spaces in Document). 41 42 6. Replace tabs with spaces (Text \> Convert \> Tabs to Spaces). 43 44 7. Save the current document (Bundles \> Formatting \> Save Current 45 Document). 46 47 8. Stop recording the macro (Bundles \> Macros \> Stop Recording). 48 49 9. Save the macro (Bundles \> Macros \> Save Last Recording). Call it 50 something like "Strip Whitespace." 51 52 10. Click in the Activation (Key Equivalent) text field and hit 53 Command+S. 54 55 Alternatively, we've packaged the bundle up and put it up on 56 [GitHub](https://github.com/vigetlabs/whitespace-tmbundle/tree/master). 57 Instructions for setting it up are on the page, and patches are 58 encouraged. 59 60 ### How About You? 61 62 This approach is working well for me; I'm curious if other people are 63 doing anything like this. If you've got an alternative way to deal with 64 extraneous whitespace in your code, please tell us how in the comments.