Delete first word of each line

Going for cryptic here, in true vi style:

1Gq10dwjq100000@1

Randy fixed this up in the comments to work on more than 100000 lines:

ggqqdwj@qq@q

For those starting out with Vim, this breaks down to:

gg    - Go to first line
qq    - Record a macro into register 'q'
dwj@q - The macro:
            dw - delete word at cursor
            j  - go down one line
            @q - run the macro in register 'q'
q     - Stop recording the macro
@q    - Execute the macro in register 'q'

In essence, the macro is recursive - it deletes a word, moves down a line, then calls itself again, repeating for each line until end of file. The final '@q' is the initial (manual) call needed to set the macro off on every line.


: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.

Tags:

Vim