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...)
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.
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.
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 @@.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With