Monday, August 14, 2006

vim :: repetition counter (and period command)

One of the funniest (and obscure) VIM features is repetition counter. Most of the commands allow the counter. Consider example (that needs to be typed in in normal mode):

20l

The VIM's command 'l' is the command to move left (VIM supports motion keys as in old time games - 'h', 'j', 'k' and 'l' are correspondently left, down, up and right.) Prepended to it '20' is the repetition counter. If cursor was in position 1, after the command it would be in position 21 (or on end of line if it is shorter).

1000dd

Would delete 1000 lines starting from current.

3f<Space>

Would move cursor to third space in line, counting from current cursor position. (:help f for find motion command.)

It is very useful at times for example combined with command '.' which is used to repeat last command(s). Example how to make a nice C comment with 50 dashes.
<Insert>/*<Space><Space>*/
<Esc>lll
<Insert>-
<Esc>49.

I intentionally made typing of dash separate, thus it can be later repeated with '.' command. I yet to gain complete understanding on how '.' works precisely, but in most obvious cases it works as expected. :help . for more info. Experimentation plus 'u' undo command could help to get it right.

More info can be found in VIM under :help count & :help repeat.txt.

2 comments:

Anonymous said...

Dunno if you'll ever read this, but, instead of typing one '-' and then repeating it 49 times with '.' you can just type 49i (to go into insert mode), type '-', then when you hit Esc to leave insert mode, it will do 49 times what you just inserted.

Ihar Filipau said...

> Dunno if you'll ever read this

Already did ;)

Thanks for great tip!

Also, inspired by your comment, I have finally forced myself to read the :help insert.txt page. Not completely of course, but still I have found what I was looking for - ^O in insert mode (:help i_CTRL-O).

In insert mode type '-', press ^O followed by 49. - and that will do precisely the same thing. But w/o all clutter of leaving insert mode and with fewer keystrokes.

You hint is better for work done mostly in command/normal mode. The ^O thing is better for work mostly done in insert mode.