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

VimCommandRepeat

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

:help repeating will provide the typical repeat commands (like ., @:, etc.). You could try repeat.vim. That may get you closer to what you are looking for.

Solution 3 - Vim

For motion commands there is no mechanism built into Vim. The Find and To commands (f/F/t/T) have ; and , to repeat and reverse. There are a couple of plugins which extend those bindings to repeat other motion commands:

http://www.vim.org/scripts/script.php?script_id=2174

http://www.vim.org/scripts/script.php?script_id=3665

The later should support repeating gE using ;

Solution 4 - Vim

You can just use "."

Example: You have "abc" at 10 places in your file and you want to replace it with "def" at 5 places of it.

Step 1: Find first occurance of abc by typing command "/abc"
Step 2: Once cursor is on "abc", Replace abc by command "cw" to take out word "abc"
Step 3: Type in "def" as replacement and press enter to go to command mode
Step 4: To repeat this action just type command "n" to go to next occurrence of abc and type command "." . The command remembers that you replaced "abc" with "def" last time and will perform the same here.

Solution 5 - Vim

You can map @: to some key for more convenience:

:map <F2> @:

and then it's easier to use it with repeats.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohvView Question on Stackoverflow
Solution 1 - VimRavikiranView Answer on Stackoverflow
Solution 2 - VimMatt GloverView Answer on Stackoverflow
Solution 3 - VimjpnpView Answer on Stackoverflow
Solution 4 - VimMananView Answer on Stackoverflow
Solution 5 - ViminsiderView Answer on Stackoverflow