Fork me on GitHub

remove trailing whitespace

02 Dec 2010

Richard Huang

I can't remember how many times the trailing whitespace makes noises in my git commits, like

alt text

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:
    " Clean up: restore previous search history, and cursor position
    let @/=_s
    call cursor(l, c)
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.