Vim: how to remove trailing whitespace on save

Tyler Tyler (258)
0

Trailing whitespace can be a real pain - especially when diffing changes between two versions of a file. Fortunately for vim users, there is an easy way to eliminate unwanted trailing whitespace without having to think about it.

Posted in these interests:

vim
PRIMARY
15 subscribers
webdev
83 subscribers

Your .vimrc file is typically located in your home directory. So you can edit it by typing:


vim ~/.vimrc


Then adding the following line to your .vimrc will ensure that every time you save a file (:w), it will remove all trailing whitespace.


autocmd BufWritePre * :%s/\s+$//e