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


Solution 1:

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:

: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:

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 ;