Removing trailing spaces with vim

Jul
16
2008
Tagged in , &
Baby Tux

After recently reading about how great VIM is for the three hundredth and fifty second time (I kept count), I decided to take a look.

It really is quite cool! I've also decided to try to log any cool tips I learn about it. Here is the first which I found after running the coder module on one of my modules (Page Title 2) and it threw hundreds of errors about too many trailing spaces on empty lines.

How to clear trailing spaces using VIM

After a bit of googling, it turned out to be REALLY easy!

:%s=\s\+$==

And thats it!

  • The % tells it to be global - not just the current line.
  • S is a shortcut for substitute.
  • The \s (whitespace) and \+ (at least once) are regular expression terms (the + needs to be escaped, it seems…).
  • The $ (dollar) represents the end of a line.
  • The = are being used as delimiters, the == at the end implies that the pattern is replaced with nothing.
Comment Icon

3 Comments

The most recent comment was on Tue, 5th Aug 2008, 08:31

That really a VIM feature?

Just a regular expression. Can be done in any editor that supports them.

KDE - KWrite
Eclipse
or just use UNIX grep tool.

Yup!

I completely agree that there are a near infinite number of tools to do REGEX replacement in files (although I believe GREP is search only, not replace... could be wrong though).

The reason I pointed this out is that you can actually use VIM as an editor and you can do this while editing the code rather than having to switch out to a different tool.

The other nice thing about VIM is that because it is (usually) server side install, you dont need to worry about installing and configuring the same (sometimes expensively licensed) software on all the machines you use (eg, work, home, laptop, friends PC if you need to do some emergency maintenance while away from office, etc).

Vim

Vim is a good text editor used under Linux platform. The editing facilities are good and it is more comfortable than the vi editor. It is a flexible tool and can be used by all- experts as well as novices.
-----------------------
Vanessa

Massachusetts Treatment Centers

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <pre>, <code>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.

Follow Me

Recent comments

Answers 2 days ago
Replies.... 1 week ago
Or in 1 week ago
A few tweaks 1 week ago
Nice 1 week ago
Thanks a million 1 week ago
Syndicate content