29

In Vim, I usually want to repeat some series of commands some times. Say, I want to comment 5 lines, I would use

I//<Esc>j
.j.j.j.j

Is there any way to repeat the last ".j" part several times?

Martín Fixman
  • 9,055
  • 9
  • 38
  • 46
  • 1
    You mean something like `5`,`j`? – C. Ross Jul 13 '10 at 20:23
  • This is a round-about answer to your needs, if not the direct question. What you seem to be doing here is commenting 5 lines of code. If this is your only requirement, then you should use the NERD Commenter add-on. This add on allows you to type `5\cc`, which comments the current line and the 4 following lines of code. `5\cs` would do the same, but insted of adding `//` line comments it will add `/*` and `*/` to block-comment your lines. Much more versatile. – cartbeforehorse Nov 11 '13 at 13:00

7 Answers7

45

One way to do this is to assign your key sequence to a macro, then run the macro once followed by the @@ run-last-macro command. For example:

qa.jq@a@@

If you know how many times you want to repeat the macro, you can use 4@@ or whatever.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
16

You can visually select the lines you want to repeat it on, type :normal! . to make vim use . on each line. Because you started with a visual selection, it ends up looking like this:

:'<,'>normal! .

However, if you're adding and removing // comments alot, you might find the following mappings useful:

" add // comment with K
noremap K :s,^\(//\)\=,//,e <BAR> nohls<CR>j
" remove // comment with CTRL+K
noremap <C-K> :s,^//,,e <BAR> nohls<CR>j

You can use 5K to comment 5 lines, you can use visual mode to select your lines first, or you can just hammer K until you've commented everything you want.

too much php
  • 88,666
  • 34
  • 128
  • 138
12

Regarding your specific example, I prefer to do multiple-line insertion using visual block mode (accessed with Ctrl-v). For example, if I had the following lines:

This should be a comment.
So should this.
This is definitely a comment.
Is this a comment? Yes.

I'd go to the top first character in the top line, hit Ctrl-v to enter visual block mode, navigate to last line (maybe using 3j to move down 3 lines, maybe using 4g to go directly to 4th line, or maybe simply G to go the end), then type I// <esc> to insert the comments on all the lines at once:

// This should be a comment.
// So should this.
// This is definitely a comment.
// Is this a comment? Yes.

Also, there's a very handy commenter/un-commenter plugin that supports many languages here. It's easier than manually inserting/removing comments.

michaelmichael
  • 13,755
  • 7
  • 54
  • 60
  • I use this one: http://vim.sourceforge.net/scripts/script.php?script_id=1173 Pretty nice. – ThePosey Jul 14 '10 at 01:15
  • Hmm, this `Ctrl-V` method doesn't work for me... Only inserts on the first line. Maybe it's because I'm still on vim 7.2... – weronika Jun 03 '12 at 06:22
  • 1
    No, plain vim... Actually I think it wasn't working for me earlier because I was exiting with Ctrl-C instead of Esc and not realizing it was different. It works now. (Although I still don't know why it works with I/A/s but not i/a...) – weronika Mar 30 '13 at 00:33
5

Try this:

  1. Do something

  2. Exit to normal mode

  3. Type, for example, 22.

The last commands will repeats 22 times.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
3

For your particular example. you could also use a range .,.5s#^#//# (to do this and the next 5 lines) or a visual block (hit v, then select the text you want) followed by :%s#^#//#.

daryn
  • 926
  • 5
  • 11
3

Another way to do it is to set marks and run substitutions over that range:

ma
jjjj
mb
:'a,'bs,^,// ,
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
0

You can repeat a macro by appending a count before the macro. For example, if you recorded a macro to the a register and you wanted to perform it five times, you would type this:

5@a
unrealapex
  • 578
  • 9
  • 23