Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the last word from each line in vim?

Tags:

vim

Please let me know, How I can remove the last word from each line using vim commands? Before :

abcdef 123
xyz 1256
qwert 2
asdf 159

after :

abcdef
xyz
qwert
asdf

Similarly please let me know how to remove the second word from each line using vim command?

like image 707
imbichie Avatar asked Apr 23 '14 06:04

imbichie


1 Answers

Use the following regex (in ex mode):

%s/\s*\w\+\s*$//

This tells it to find optional whitespace, followed by one or more word characters, followed by optional whitespace, followed by end of line—then replace it with nothing.

like image 51
Sarah G Avatar answered Sep 20 '22 13:09

Sarah G