Remove trailing whitespace with VIM

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

Let’s say, I have the following code:

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


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

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

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

One thought on “Remove trailing whitespace with VIM

  1. Jay

    can you also explain what `%` does? i know, but it’s good for other readers to understand why it’s there.

    also you may consider to use `:%s/\v\s+$//` the `+` doesn’t need a `\` if it’s “very magic” (i.e. extended regular expression)

    not sure why you would have errors or need the `e`?


Leave a Reply

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