66

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

Community
  • 1
  • 1
johv
  • 4,424
  • 3
  • 26
  • 42

5 Answers5

96

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.

Ravikiran
  • 1,440
  • 11
  • 15
15

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

Matt Glover
  • 1,347
  • 7
  • 13
13

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 ;

jpnp
  • 1,390
  • 1
  • 10
  • 9
11

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.

Manan
  • 229
  • 1
  • 4
  • 10
3

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

:map <F2> @:

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

insider
  • 1,818
  • 2
  • 17
  • 15