Posted by
flyerhzm
on
December 02, 2010
Trailing whitespace always makes noises in version control system, it is meaningless. We should remove trailing whitespace to avoid annoying other team members.
I can't remember how many times the trailing whitespace makes noises in my git commits, like

the trailing whitespace makes no sense and it always annoys other team members.
To collaborate better with others, we should form a habit to remove trailing whitespace before committing. There are a lot of ways to do it,
we can remove whitespace by ourselves, but it's easy often to forget to do as the trailing whitespace is invisible by default.
we can set a git pre commit hook to detect/remove trailing whitespace, that's fine and please remember to use the pre commit hook for every projects.
The best way is to remove trailing whitespace before saving a file within your ide. I prefer using Vim and TextMate, here's my solution.
For TextMate, you can just install uber-glory-tmbundle, it's really easy.
For Vim, you should add the following codes in your ~/.vimrc file.
" Strip trailing whitespace
function! <SID>StripTrailingWhitespaces()
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
%s/\s\+$//e
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
There are similar solutions to other ides (Emacs, Netbeans, etc.), but I don't use them. If you know, please write a comment, thanks.
After your team all set up removing trailing whitespace in your ides, you will never see the trailing whitespace noises in git commits again.

Comments
" remove empty lines at end of file
%s/\($\n\s*\)\+\%$//e
autocmd BufWritePre * :%s/\s\+$//e
Windows user should give Kate a try ;)
1. class User < ActiveRecord::Base
2. belongs_to :group
3.
4. has_many :posts
5. end
Do you recommend to remove trailing whitespace in line 3? Or just after non-empty lines?
As far I'm concerned, I remove trailing whitespace after non-emtpy lines, and to remove empty lines at the end of file, but not on other trailing whitespace. On the contrary, I put as much whitespace as the previous line.
find . -name *.*rb -exec sed -i "" 's/[ ]*$//' {} \;While it's "best practice" with new projects it might lead to extra headache when contributing into existent projects: http://stackoverflow.com/questions/3515597/git-add-only-non-whitespace-changes
And there a lot projects ignoring this practice...
According my experience it's better to highlight trailing white spaces in vim and have separate tool to clear them all using separate commit when/if others team members accept such "no-whitespace" policy.
it allows me to type \l (leader being \ by default) and it shows me trailing white spaces and non breaking spaces with # signs in black on red, you can't miss them. (it also shows me tabulations as ▸–––... so that I can go and replace them with spaces if I like)