VIM: How to remove 3 words from the end of a line in normal mode?

What is the shortest combo?

PS I usually do A Ctrl + w Ctrl + w Ctrl + w Esc .
In other words, I have to go into Insert mode.

PPS d 3 b does not work.

+8
vim
source share
5 answers

This sequence of commands,

$3bD 

is the shortest way to do this that I can think of at the moment, but it leaves an ugly finite space.

 $3gelD 

- This is another way to get rid of the finite space.

You can also think outside the box and play with spaces instead of words:

 $3FD 
+7
source share

$ 3 b D , which translates to

  • $ to end of line
  • 3 three of the following:
  • b back three words [one without modifier]
  • D delete the characters under the cursor to the end of the line

Alternatively, you can also use the normal command in ex mode to achieve the same

 :norm! $3bD 
+9
source share

$ 3bd

or

$ 3bDx

... if you also want to remove the trailing space.

+4
source share

Go to the end of the line, return 3 words, then delete to the end of the line: $ 3 b D

or

Go to the end of the line, delete 3 words, then delete the extra character: $ 3 D b x

+4
source share

As others noted, $3bD is the shortest, and $3bDx will be enough to remove the remaining spaces, considering it exactly one space or tab.

You can use $3gelD to account for all spaces. This option has the advantage of clearing in most cases, but it fails when the line contains exactly 3 words, because the 3ge movement returns to the previous line.

An alternative approach is based on daw . Since 3daw acts forward, you must use $daw.. or $3b3daw . For 3 words, $daw.. shorter and probably more desirable, but $3b3daw larger than Vim-esque and can easily be expanded to more words.

b , ge and w can be replaced with b , ge and w to remove WORD instead of words.

+4
source share

All Articles