38

In Unix the ^ allows you to repeat a command with some text substituted for new text. For example:

csh% grep "stuff" file1 >> Results
grep "stuff" file1
csh% ^file1^file2^
grep "stuff" file2
csh%

Is there a Vim equivalent? There are a lot of times I find myself editing minor things on the command line over and over again.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Whaledawg
  • 4,234
  • 4
  • 26
  • 21

8 Answers8

97

Specifically for subsitutions: use & to repeat your last substitution on the current line from normal mode.

To repeat for all lines, type :%&

LearnOPhile
  • 1,391
  • 13
  • 16
11

q: to enter the command-line window (:help cmdwin).

You can edit and reuse previously entered ex-style commands in this window.

rampion
  • 87,131
  • 49
  • 199
  • 315
  • Not exactly what I was looking for, I wanted a quicker way. But this is really good info because now I could at least write a function to do what I'm looking for. Thanks. – Whaledawg Dec 19 '08 at 21:23
9

Once you hit :, you can type a couple characters and up-arrow, and it will character-match what you typed. e.g. type :set and it will climb back through your "sets". This also works for search - just type / and up-arrow. And /abc up-arrow will feed you matching search strings counterchronologically.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
dkretz
  • 37,399
  • 13
  • 80
  • 138
7

There are 2 ways.

  1. You simply hit the . key to perform an exact replay of the very last command (other than movement). For example, I type cw then hello to change a word to "hello". After moving my cursor to a different word, I hit . to do it again.
  2. For more advanced commands like a replace, after you have performed the substition, simply hit the : key then the up arrow key, and it fills your command line with the same command.
Flimm
  • 136,138
  • 45
  • 251
  • 267
palehorse
  • 26,407
  • 4
  • 40
  • 48
  • 1
    But I don't want to repeat the command, I want to repeat a command(from the command line) with slightly different text. – Whaledawg Dec 19 '08 at 22:11
  • Yes, you can the ^ arrow, then change anything on the line. I am sorry I did not mention that. – palehorse Dec 19 '08 at 22:21
7

To repeat the previous substition on all lines with all of the same flags you can use the mapping g&.

ruohola
  • 21,987
  • 6
  • 62
  • 97
  • 1
    This is close to what I'm looking for - if I make a substitution (`:s/A/B/g` on the current line in normal mode or `v`/`V`...`:'<,'>s/A/B/g` for a range of lines in visual mode), I want to repeat the substitution on another line/selection, preferably in the mode of my choice. Thanks to your response, I found that from normal mode `&` (equiv. `:&`) will substitute on the current line and `:'<,'>&` (`v`/`V`...`:&`) will substitute on the range of lines selected in visual mode. Both versions reapply the last substitution used in either normal or visual mode. I'll post this as an answer too. – John P Oct 12 '20 at 00:51
3

If you have made a substitution in either normal mode :s/A/B/g (the current line) or visual mode :'<,>'s/A/B/g (lines included in the current selection) and you want to repeat that last substitution, you can:

  • Move to another line (normal mode) and simply press &, or if you like, :-&-<CR> (looks like :&), to affect the current line without highlighting, or

  • Highlight a range (visual mode) and press :-&-<CR> (looks like :'<,'>&) to affect the range of lines in the selection.

With my limited knowledge of Vim, this solves several problems. For one, the last visual substitution :'<,'>s/A/B/g is available as the last command (:-<UP>) from both normal and visual mode, but always produces an error from normal mode. (It still refers to the last selection from visual mode - not to the empty selection at the cursor like I assumed - and my example substitution exhausts every match in one pass.) Meanwhile, the last normal mode substitution starts with :s, not :'<,'>s, so you would need to modify it to use in visual mode. Finally, & is available directly from normal mode and so it accepts repetitions and other alternatives to selections, like 2& for the next two lines, and as user ruohola said, g& for the entire file.

In both versions, pressing : then & works as if you had pressed : and then retyped s/A/B/, so the mode you were in last time is irrelevant and only the current cursor line or selection determines the line(s) to be affected. (Note that the trailing flags like g are cleared too, but come next in this syntax too, as in :&g/: '<,'>&g. This is a mixed blessing in my opinion, as you can/must re-specify flags here, and standalone & doesn't seem to take flags at all. I must be missing something.)

I welcome suggestions and corrections. Most of this comes from experimentation just now so I'm sure there's a lot more to it, but hopefully it helps anyway.

John P
  • 1,463
  • 3
  • 19
  • 39
0

Take a look at this: http://vim.wikia.com/wiki/Using_command-line_history for explanation.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
0

You can type @: to repeat the last command.

namti
  • 31
  • 3