Vim remove trailing spaces

To remove all trailing spaces in vim, use the following command:

:%s/ \{1,}\n/\r/gc


: - command
%s - entire selection
/ - separator
\{1,}\n - search for one or more spaces followed by a newline
/ - separator
\r - replace with a newline
/ - separator
g - global replace
c - confirm replacements (type y (yes) to confirm replacements)
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .


  1. anonymous

    Why don't you use

    :%s/ \+\n/\r/gc

  2. anonymous

    @"Why don't you use"

    both work. which one reads more clearly?

Leave a Reply