3

In Vim as probably all of you know it is possible to give a count to commands in "normal mode" by preceding them with a number.

Like for example 4dd that will delete 4 lines.

I am wondering if there is a way to pass a count to a vim command line command (a command beginning with colon) to repeat it multiple times.

Something like 2:normal oHi! to repeat the normal oHi! command 2 times.

Is there a way to do something like this within Vim?

3

Many :-commands do accept a count as an argument. For example:

:delete 4

Would delete four lines, starting with the line the cursor is on.

Some :-commands take a count before the command name instead:

:2next

Would edit the second next file in the argument list.

However, the :normal command does not accept a count in either form. You would have to wrap it in a loop:

for i in range(1,2)
    normal oHi!
endfor

And of course you could create a user-defined command that did what you wanted:

command -count=1 -nargs=+ Normal for i in range(1,<count>) | exe 'normal ' . <q-args> | endfor

The -count=1 sets the default count to 1, and you invoke it the second way:

:2Normal oHi!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.