Reputation: 23341
How can I bring back the last string I used for a search or a search&replace?
For example, assume that I enter :%s/some_text/some_other_text/gc
and vim gives me the E486: Patterns not found: some_text
error message back. I then realize that I actually meant to write some_magic_text
instead of some_text
. At that point, how can I get back my original string in the bottom command row (or whatever it is called) so I can change it and do a second search? Is there a nifty little command for that?
In this brief example it looks unnecessary, but when the text you are looking to replace is mighty long and you just typed one letter wrong, it is fantastically annoying to have to retype everything.
And I am using MacVim if that makes any difference.
Upvotes: 15
Views: 7991
Reputation: 19667
This answer might be good an improvement to what you are after, after all.
Use search with highlighting, to interactively check if the regex you are crafting is definitely working, and then use it in a search-replace.
:se is
(incsearch
, better put se is
in your .vimrc)/<search term>
n
/N
if you are happy with the matches:s%//<replace term>/g
When omitting the <search term>
in the search-replace in 4.
, the last used search will be used.
For acessing the list of last (search-replace) commands use q:
, or as already noted q/
for the list of last search terms.
Bonus:
When using :se gd
, s/<search>/<replace>
will behave as s/<search>/<replace>/g
.
Accessing just the first search match in each line can then still be done with adding /g
, so essentially both behaviours are just switched.
Upvotes: 4
Reputation: 79165
From the normal mode, hit q/
to navigate through your search history!
Upvotes: 26
Reputation: 42198
More generally, you can recall any command you have previously typed by entering the first few characters, and then use arrow multiple times to navigate in history.
In your case, you could type:
:%s<Up>
See :help history
Upvotes: 4