delladea: (Default)
Geeky Muse ([personal profile] delladea) wrote in [site community profile] dw_dev_training2012-02-03 01:52 pm
Entry tags:

Strip tab characters from multiple text files and replace them with spaces (or something else)

I switch between Gedit, Notepad++, and vim fairly often depending on what I'm doing and whose computer I'm on. Sometimes I end up with tab characters where I really wanted four spaces, mainly when I'm using vim and I haven't figured out how to get vim to not do this. Gedit and Notepad++ have settings to use spaces instead of tabs, so there's no issue there.

Either I don't notice the tab characters until after I've put lots of them in the file I'm editing, or I'm editing a file from someone else whose editor uses tab characters for indentation. I know its not a big deal to some people, but tab indentation mixed with space indentation is a huge pet peeve of mine.

Thus, a perl script was born:


View Gist (strip-tabs.pl)

Feel free to gank away if you find it useful!
shadowspar: Pic of Kurama holding a rose (kurama - rose)

[personal profile] shadowspar 2012-02-04 01:01 am (UTC)(link)

There's definitely a lot of ways to accomplish any given task in the *nix environment. One of the Perl mottos is "TMTOWDTI" (There's More Than One Way To Do It), but in a lot of ways that's really true of the Unix culture that Perl grew out of. =)

The settings you're looking for in vim are expandtab (convert tabs to spaces), tabstop (how many spaces a tab counts for), and shiftwidth (how many spaces an indent should be). In my case, I want a four space indent using spaces not tabs, so I have this line in my .vimrc file:

    set expandtab tabstop=4 shiftwidth=4

This article in the vim wiki elaborates a bit more.

Thanks for posting this! Tabs in source files are the bane of my existence too. ^_^;

shadowspar: Picture of ouendan (\o/)

[personal profile] shadowspar 2012-02-04 03:17 pm (UTC)(link)
Yaaaay, so glad to hear! \o/ I think there's a way to do almost anything in vim, but sometimes the path to get there can be a little bit arcane. =)