Nov 11 emacs + git dealing with whitespaces in code
tags:
Your are a good coder citizen and leave no unneeded white spaces in the code, you know so that git diff shows only worthy changes. But you have to deal with other peoples code, and they are not always good citizens.
The problem
I have the following in my .emacs to remove all trailing white spaces before saving:
;; delete trailing whitespace before save (add-hook 'before-save-hook 'delete-trailing-whitespace)
but when editing files that do have white spaces my git diff can get really bloated with unneeded lines. It’s easy detectable using git diff -w to remove unimportant lines from the diff.
emacs white space mode
Emacs come with support to make the spaces in code visible
So adding this to my .emacs I am able to detect them:
(require ‘whitespace)
(autoload ’whitespace-mode “whitespace” “Toggle whitespace visualization.” t)
(autoload ’whitespace-toggle-options “whitespace” "Toggle local `whitespace-mode’ options." t)
Next thing is before editing a file that is full of spaces I normally make commits for only removing spaces and mark them with the commit message [BLANKS] removing blanks. You just make sure git diff —cached -w shows an empty files list.
Then you agree with your team to use soft tabs in code, and the amount of spaces (two tipically). And there again M-x untabify is your friend.
So now I keep unimportant commits easily marked and I don’t waste time browsing garbage in the repo.