Removing Trailing Spaces in VIM
We all hate trailing spaces when programming, so here’s something that will rid us of that forever.
Before quitting, execute this simple find and replace command in VIM.
:%s/s+$//
What this does is it looks for one or many instances of space (s) just before the end of line ($) and replaces it with a null string.
Even better, you can have this command execute every time you save by binding this to the save command in your .vimrc (vim configuration file).
autocmd BufWritePre * :%s/s+$//e
The way I’ve always done it is:
:1,$ s/[ ]$//
In between the square brackets are a space and a tab. Than nails trailing tabs too.
The “1,$” address all lines in the file (there’s probably a more succinct way, like, ‘%s’ apparently.
It’s always instructive to watch others use vim, as each person learns their own little tricks, and tends to use vi(m) in a way that is somewhat foreign and interesting to others.
scaryreasoner
August 22, 2008 at 2:42 am
@scaryreasoner, we all stand on the shoulders of giants.
marksman
August 23, 2008 at 4:16 am