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.

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

Anonymous said...

1) If you don’t have an account already, register on PayPal ( www.paypal.com ). Its free.

2) http://www.AWSurveys.com/HomeMain.cfm?RefID=mirbabu

also free.

3) Log in AWSurvey click “Browse Ads”. Click the listed advertisements and wait until the 30-second counter finishes before closing the window. Click each Ad ONE AT A TIME. It won’t work if you just open all ads at once.
You earn money on this site by clicking ads.

4) When you have earned 75 dollars, transfer them to your PayPal account by clicking “Cashout” in your “Members” window.Now you can buy rapidshare premium account.

5) Don’t forget to come back everyday and click more ads to get more money

Leave a Reply

Anonymous said...

!!!Easy Get Money!!!
1. go to the following link; Welcome $6 Registration Free Join this site. It is also free. You get paid for completing surveys over there 6$ to 4$ dollars per survey and 1.25$ per referral . have a 1 year premium account and now i am collecting some exta cash .
2. Then create a paypal PREMIUM Account at this address. It is free; ; http://www.paypal.com . When asked for credit card details simply say cancel. You do not need to fill it . Another thing you can also use paypal money to buy stuff on ebay and other shopping sites.
3. Thats is it. So Simple and I swear it works.
4. I learnt this from another friend and i thought it will be good for my friends here. So I thought of helping
5. Try Now Click here