Strip Trailing Spaces When Saving in ViM

Hello Linux Geeksters.

Watch Free Movies

In this article I will show you what you have to do to make Vim remove the trailing spaces when you save changes in a file.

This oneliner should do the trick. Paste this oneliner in your ~/.vimrc to remove the trailing spaces when saving changes in a file:

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

If you want the trailing spaces to be removed automatically, for a certain kind of files, put this in your ~/.vimrc:

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

This deletes the trailing spaces from python files. Replace py with the extension of the file you need to have the trailing spaces removed.

Scroll to Top