Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I repeat any command in Vim, like "C-x z" in emacs?

In Vim, is there any way to repeat the last command regardless of whether it was an edit or not, and without having the foresight to first record a macro?

E.g. say I type :bn, and want to do it again (it was the wrong file). Pressing . obviously doesn't do it. Or maybe I'm doing gE and want to repeat that (with one keystroke since clearly gE is kinda painful to type).

Perhaps there are some plugins? Similar to this question.

(Even cooler would be to retroactively bind a number of commands to a macro, so one could type 5qa@a or something to repeat the last 5 commands...)

like image 351
johv Avatar asked Jan 25 '11 03:01

johv


People also ask

How do I repeat the same command in Vim?

The " @: " command repeats the last command-line change (a command invoked with " : ", for example :s/old/new/ ). You can move the cursor before using either of the repeat commands. Suppose you press dd to delete a line. Next, you might move the cursor, then press 5.

How do you repeat a command in Emacs?

The command C-x z ( repeat ) provides another way to repeat an Emacs command many times. This command repeats the previous Emacs command, whatever that was. Repeating a command uses the same arguments that were used before; it does not read new arguments each time.

How do you repeat a macro in vim?

To replay the macro once, move the cursor to the next line and press @h where h represents the register on which you saved the macro. Notice that the macro automatically moves the cursor to the next line as required. This allows you to repeat its execution. To repeat the macro execution, press @@.


1 Answers

To repeat a command-line command, try @:, To repeat a normal/insert-mode command, try .,

Add below mapping to your .vimrc if you want to shortcut the same:-

:noremap <C-P> @:<CR> - This will map Ctrl+P to previous command-line command. You can map any other combo.

like image 100
Ravikiran Avatar answered Sep 28 '22 03:09

Ravikiran