Friday, January 16, 2009

Expanding Tabs in Text Files

Text files that contain tab characters can be really annoying for programmers when switching editors or development environments. A tab character is displayed as several spaces, and the problem is that different editors have a different idea as to the number of spaces to substitute for each tab. This fact can, and does, mess up the indentation of source code.

As an emacs user (read: junkie) I always disable the "Indent Tabs Mode", to prevent indentation from inserting tabs into the code:
  1. type
    M-x customize-apropos <ENTER> indent-tabs-mode <ENTER>
    (on normal PC keyboards, the M-x stands for the key combination <ALT> x)
  2. toggle the value of this customization variable from "on" to "nil"
  3. press "Save for Future Sessions"

If you can't stand the fact that the code you're working with has tabs in it, and you don't mind causing all sorts of merge problems for your fellow developers, you can use the expand utility to expand the tabs, and replace them with spaces:
expand filename.ext > filename.ext.expanded
mv -f filename.ext.expanded filename.ext

(use the -t command line option if the default translation of one tab to eight spaces is not appropriate for the file in question).

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.