Cher's Tips and Tricks
Simple Code Reformatting
The Vim editor has some really nice features. For reformatting code, we will use the following features:
- Supplying multiple files on the command line, using a list of source files generated by something else (e.g. the find command)
- A simple but very powerful command language
- Giving startup commands with
-c - A global regular expression substitution using
%s - Writing a file using
w - Quitting vim after the job is done using
q - Configuring the tab width using
set tabstop=4 - Configuring vim to use spaces instead of tabs using
set expandtab - Retabbing files using
retab - Looping over all files executing a sequence of commands using
bufdo
Simple code reformatting can be useful in various places:
- When importing, migrating or fixing code that has tabs while it should have spaces instead. That's especially the case when importing code from broken IDEs like Eclipse or Visual Studio.
- When removing trailing whitespace from source code.
Retabbing (replacing tabs with spaces)ex -s -c "set et ts=4 | bufdo! retab | w" -c "q" $(find client server -name "*.c" -or -name "*.h") |
Removing all trailing whitespaceex -s -c 'bufdo! %s/\s\+$//e | w' -c "q" $(find client server -name "*.c" -or -name "*.h") |
Retabbing and removing all trailing whitespace in a single command lineex -s -c 'set et ts=4 | bufdo! retab | %s/\s\+$//e | w' -c "q" $(find client server -name "*.c" -or -name "*.h") |
Using kdiff3 as 3-way-merge-tool for subversion
Create the following shell script. Put it somewhere in the system. I've stored it at /home/cher/bin/kdiff3.sh.
#!/bin/bash
kdiff3 ${10} ${9} ${11} -o ${10}.merged
cat ${10}.merged
rm ${10}.merged |
Now change ~/.subversion/config to include the following line in the section [helpers]:
diff3-cmd = /home/cher/bin/kdiff3.sh |