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!
mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)

[staff profile] mark 2012-02-03 08:34 pm (UTC)(link)
Cool, thanks for sharing!

Another way of doing this is using sed, like this:

sed -ie 's/\t/ /g' yourfile.pl

That will replace all tabs with four spaces in yourfile.pl. You can do it in all files of a certain type with something like this:

find -name \*.pl -exec sed -ie 's/\t/ /g' {} \;

That will find all Perl files (starting in the current directory and going down, so it'll recurse into any subdirectories) and replace the tabs with spaces.

These work from a Linux/Mac command line. Windows, you're out of luck.
Edited 2012-02-03 20:34 (UTC)
momijizukamori: Green icon with white text - 'I do believe in phosphorylation! I do!' with a string of DNA basepairs on the bottom (Default)

[personal profile] momijizukamori 2012-02-04 01:16 am (UTC)(link)
Gedit and Notepad++ have settings to use spaces instead of tabs, so there's no issue there.

I did not know this setting existed in gedit until right now. Congratulations, you have just made my day more magical.
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)

[personal profile] pne 2012-02-06 10:21 am (UTC)(link)
Would you be interested in some comments on the Perl code and suggestions for other ways to do things?