Strip Trailing Spaces When Saving in ViM

Hello Linux Geeksters.

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.

Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!