I wasn't going to post this beyond my comment but then I see it turned into a competition. ;)
If you mistakenly enter /%s/foo/bar/
when you actually want to run :%s/foo/bar/
then you just need these eleven keystrokes:
q/kY:q<CR>:@0<CR>
Mapped:
nnoremap <F10> q/kY:q<CR>:@0<CR>
I guess I should explain. In the search history window (q/
) we move to (k
) and yank the line containing the last search with Y
(short for yy
). A yank without a register associated with it always goes to register 0
. Leaving the search history window (:q<CR>
) for the the regular command line (:
) we use the "execute register contents" command (@
) with said register like so: @0
. Execute it (<CR>
) and you're done.
A bit of background on why the (relatively) obvious solution won't work...
Those who are aware that the most recently entered search pattern gets saved in register /
may wonder, "Why can't you just transfer what was entered at the /
prompt to the normal command line by dumping the /
register there (i.e. :<C-R>/<CR>
)?"
The problem is the slash between %s
and foo/bar/
. This fools Vim into thinking you are entering the command that has form /{pattern}/{offset}<CR>
(see :h search-commands
). As a result it interprets the text after that slash, i.e. foo/bar/
, as an invalid entry for the {offset}
part. Valid or invalid {offset}
is never saved in the /
register. Only {pattern}
is. (Side note: :h {offset}
shows valid values for that parameter.)
Any workarounds?
Well, if OP Quasimodo was in the habit of entering substitution commands with a valid separator other than /
(e.g. #
or @
) then they never would have gotten themselves in this mess because their original thought to use <C-R>/
would likely work then. ;)
q/
to retrieve the whole thing.q/
out at the same time!/
rather than:
in my brain. Apparently I am not the only one.