Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a command with substitution in Vim?

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.

like image 542
Whaledawg Avatar asked Dec 19 '08 20:12

Whaledawg


People also ask

How do you repeat commands in Vim?

vim Normal mode commands (Editing) Repeat the Last Change Your cursor will be at position 1 of line 1, and all you need to do to fix the next two lines is press j. twice - that is, j to move down a line and . to repeat the last change, which was the addition of the I .

How do you replace one word with another in Vim?

Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.

How do you substitute in vi?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y , and to scroll up, use CTRL+E .

Does Vim use sed?

1 Answer. sed and vim are not related (well sort of, but it's far out). Their substitution commands are similar, but far from identical. Both of them use basic regular expressions, but vim has its extensions for it, and GNU sed has other extensions for it.


3 Answers

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

To repeat for all lines, type :%&

like image 79
LearnOPhile Avatar answered Oct 17 '22 03:10

LearnOPhile


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

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

like image 28
rampion Avatar answered Oct 17 '22 01:10

rampion


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.

like image 9
dkretz Avatar answered Oct 17 '22 02:10

dkretz