Before you commit changes to a SVN repository, you might want to replace tabs by spaces in source code. Some text editor can be configured in order to do it automatically during the edition of the file:
- Gedit: In the Edit / Preference / Editor menu check the “insert spaces instead of tabs” and set it to as many space characters as you want.
- Eclipse: In the Window / Preference / menu and under the “General / Editor / Text Editor” section set the “Displayed tab width” value to what suits your needs and check the “insert spaces for tabs” checkbox.
But sometimes you’re using another editor without this option or you copy paste code from a source that contains tabs characters. In this case you can use this script:
Here is a script that can help you:
#!/bin/bash
basename=${1##*/}
expand --tabs=2 $1 > /tmp/$basename
cp /tmp/$basename $1
rm /tmp/$basename
The “–tabs=2″ option set the numbers of spaces to replace a tab.
Call the script with the filename in argument:
$ tab2space.sh myfilewithtabs.cxx
If you need this only once in your life you might want to consider using this free online tool instead: http://www.textfixer.com/tools/remove-white-spaces.php
Update Apr, 27th:
http://www.commandlinefu.com/commands/view/2312/find-and-replace-tabs-for-spaces-within-files-recursively
A one liner to replace all the tabs by spaces in all the files of a folder recursively:
$ find ./ -type f -exec sed -i 's/\t/ /g' {} \;
Adjust the number of spaces you want in the sed expression (replace X by space): ‘s/\t/XX/g’ for 2 spaces, ‘s/\t/XXXX/g’ for 4 spaces