tags:

views:

509

answers:

5

how to delete first word of each line in vim?

how about pattern on each line

+2  A: 

First word (where word is defined as no whitespace)

:%s/^\s*[^ ]* //g

Delete pattern:

:%s/< insert pattern here >//g
John Weldon
+6  A: 

I would use something like the following:

:%s/^\w+\s+//

The regular expression will match one or more "word" characters starting at the beginning of the line followed by at least one whitespace character. It will remove the word and any following whitespace. If a line can contain only a single word -- and you still want it removed -- you could use alternation to match either whitespace or the end of line.

:%s/^\w+(\s+|$)//
tvanfosson
You can express this more generally::%:s/^\(\w\{-1,}\)\(\W\?\)/\2/This way you also don't throw away whatever terminates the word character.(You also need to watch which dialect of regexp you're speaking...)
Alex Feinman
+2  A: 

Going for cryptic here, in true vi style:

1Gq10dwjq100000@1
Alex Feinman
-1 - not sure what's "true vi style", but there is no point in doing cryptic, unfriendly things, just to show off. there is perfectly readable solution, which is also more general.
depesz
Ah, but someone's already posted the 'correct' solution. This was more along the lines of code golf.The "true vi style" was an ironical swipe at an editor where :wq! is the correct way to exit without changes. It's not the most user-friendly beast. Full disclosure: I love vi and use it as my main editor. Full disclosure: vi command syntax is not very understandable.
Alex Feinman
ggqqdwj@qq@q will work on any number of lines, not just files with lines < 100000. Tbh, this 'cryptic' was was what came first in my mind over a regular expression.
Randy Morris
A: 

What about this?
:%!cut -s -d' ' -f2-

Vereb
+1  A: 

:normal to the rescue:

:%norm dw

It basically replays the arguments as if you were typing them in normal ('non-edit') mode.

From :help :

:norm[al][!] {commands}

Execute Normal mode commands {commands}.

This makes it possible to execute Normal mode commands typed on the command-line. {commands} is executed like it is typed.

Leonardo Constantino
How does it work?
vehomzzz
I just added more info to my response.
Leonardo Constantino