Here’s a quick tip on how to get rid of unwanted trailing whitespace using vim.

Let’s say, I have the following code:

trailing_whitespaces.png

Open that file with vim. The following command deletes any trailing white spaces.

:%s/\s\+$//e

Here’s the screenshot of the text after removing the whitespace:

trailing_whitespaces_after.png

In search, \s finds whitespace (a space or tab). \+ finds one or more occurrences. $ matches the end of line, finally e flag means no error is displayed.

Read more:

  • Remove unwanted spaces (warning: this reference page contains lots of ads, probably avoid opening it on a mobile device.)