0

I have:

  int x = 1;______

(underscores means spaces) and I would like to get:

  int x = 1;

My naive solution is $bld$, is there a quickest way?

In Emacs I use M-\ (delete-horizontal-space)

o3o
  • 1,094
  • 13
  • 23
  • See also [How can you automatically remove trailing whitespace in vim](http://stackoverflow.com/questions/356126/how-can-you-automatically-remove-trailing-whitespace-in-vim) – Chris Morgan Jun 22 '13 at 00:35
  • 1
    @doubleDown It's a sequence of key presses. $ moves the cursor to the end of the line, b moves the cursor to the end of the previous word, l moves the cursor one character to the right and d$ deletes everything from the cursor to the end of the line. – David Brown Jun 22 '13 at 17:37

3 Answers3

2

For the current line:

:s/\s\+$

For all lines:

:%s/\s\+$

The substitution text can be omitted if blank, so we don't need to write s/\s\+$//.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • 1
    `\+` is more efficient than `*`; it will mean that only lines with trailing whitespace will get altered, rather than every line. – Chris Morgan Jun 21 '13 at 23:52
  • I'd put it much stronger. `*` is simply a bad idea. Try `u` after it: every line has been modified, rather than just the lines which have actually been altered. Makes it hard to see what changed. – Chris Morgan Jun 22 '13 at 00:12
1

I do this with a search and replace mapping:

map <leader>W :%s/\s\+$//<CR>:let @/=''<CR>

:%s/\s\+$// deletes all trailing white space and then :let @/='' clears the search register.

David Brown
  • 13,336
  • 4
  • 38
  • 55
0
:%s/\s\+$//

What it does is it searches for white spaces at the end of the line and replace them by nothing.

Source: http://vim.wikia.com/wiki/Remove_unwanted_spaces