There is no reason to leave trailing white-space characters in your project's files, so don't add some. A git diff will usually highlight them and you should not commit when you see them. So go ahead and switch your editor/IDE to automatically remove them for you. Below are a few instructions on how to get them removed by your favorite IDE or editor. Note that except for Older Version of RubyMine(v <= 5.4.3 ), the following changes will remove trailing white-space on all lines, not only those that you changed. While this should not be a problem if your project is always clean of trailing spaces, it may not be what you want if you are in the wild west. In that case you need to take care not to commit trailing spaces. Always git diff, kids! RubyMine : • Settings (Ctrl-S) • Pick "Editor" (in the "IDE Settings" section) • In the bottom left, set "Strip trailing white spaces on Save" to "Modified Lines" Vim: • Open up your ~/.vimrc file • Add this:
autocmd BufWritePre * :%s/\s\+$//eTextMate • The Text bundle offers a command Remove Trailing Spaces in Document/Selection. • In the bundle editor, assign it a shortcut like Cmd + Alt + Backspace. • Update: Since 9-29-2012, TextMate2 has a callback will-save. In the above command, set 'Semantic class' to callback.document.will-save, and it will be called before saving the document. If that is too invasive for you, try only highlighting trailing spaces like suggested in the Vim wiki (also put this into your ~/.vimrc):
Show trailing whitepace and spaces before a tab: :highlight ExtraWhitespace ctermbg=red guibg=red :autocmd Syntax * syn match ExtraWhitespace /\s\+$\| \+\ze\t/Git: • There is a thread on Stackoverflow about a git pre-commit hook. To use it, add the following to .git/hooks/pre-commit.
#!/bin/sh # # A git hook script to find and fix trailing whitespace # in your commits. Bypass it with the --no-verify option # to git-commit # if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then against=HEAD else # Initial commit: diff against an empty tree object against=er4433dd fi # Find files with trailing whitespace for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | (sed -r 's/:[0-9]+:.*//' > /dev/null 2>&1 || sed -E 's/:[0-9]+:.*//') | uniq` ; do # Fix them! (sed -i 's/[[:space:]]*$//' "$FILE" > /dev/null 2>&1 || sed -i '' -E 's/[[:space:]]*$//' "$FILE") git add "$FILE" done # Now we can commit exitThanks to Santosh for writing this post . Post Comments And Suggestions !!