How to repeat a command with substitution in Vim?
Asked Answered
H

8

40

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.

Howlyn answered 19/12, 2008 at 20:37 Comment(0)
V
102

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

To repeat for all lines, type :%&

Vitus answered 18/7, 2013 at 13:48 Comment(2)
This also should also be the accepted answer of this question.Jinajingle
To repeat the previous replace on all lines with confirmation, you can do :%&cMartamartaban
M
11

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

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

Marquettamarquette answered 19/12, 2008 at 21:12 Comment(1)
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.Howlyn
B
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.

Biotechnology answered 19/12, 2008 at 21:27 Comment(0)
M
8

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

Monomer answered 11/4, 2020 at 21:43 Comment(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.Dow
S
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.
Senzer answered 19/12, 2008 at 21:4 Comment(2)
But I don't want to repeat the command, I want to repeat a command(from the command line) with slightly different text.Howlyn
Yes, you can the ^ arrow, then change anything on the line. I am sorry I did not mention that.Senzer
D
4

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.

Dow answered 12/10, 2020 at 6:28 Comment(0)
U
3

You can type @: to repeat the last command.

Unrobe answered 15/3, 2023 at 23:13 Comment(0)
B
1

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

Bonaparte answered 19/12, 2008 at 20:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.