The Coded One

programming, algorithms, discrete math, open-source

Removing Trailing Spaces in VIM

with 2 comments

We all hate trailing spaces when programming, so here’s something that will rid us of that forever.

Before quitting, execute this simple find and replace command in VIM.

:%s/s+$//

What this does is it looks for one or many instances of space (s) just before the end of line ($) and replaces it with a null string.

Even better, you can have this command execute every time you save by binding this to the save command in your .vimrc (vim configuration file).

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

Written by Mark Basmayor

August 22, 2008 at 2:14 am

Posted in Linux, Ubuntu

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. The way I’ve always done it is:

    :1,$ s/[ ]$//

    In between the square brackets are a space and a tab. Than nails trailing tabs too.

    The “1,$” address all lines in the file (there’s probably a more succinct way, like, ‘%s’ apparently.

    It’s always instructive to watch others use vim, as each person learns their own little tricks, and tends to use vi(m) in a way that is somewhat foreign and interesting to others.

    scaryreasoner

    August 22, 2008 at 2:42 am

  2. @scaryreasoner, we all stand on the shoulders of giants.

    marksman

    August 23, 2008 at 4:16 am


Leave a comment