How do I rerun a previous search pattern from history directly on the / prompt?
Answer
/\V<C-r>=histget('/', -2)<CR>
Explanation
If you often alternate between two complex search patterns, opening q/ each time is slow. This trick pulls an older search entry directly into the / prompt using histget(), so you can replay history inline and keep your flow.
How it works
- Start a normal forward search with
/ \Vswitches to very nomagic mode so pasted patterns are treated literally unless you intentionally include regex atoms<C-r>=...<CR>evaluates an expression on the command linehistget('/', -2)fetches the second most recent search entry from search history (-1is most recent)
This is particularly useful when you are toggling between two review targets, such as a symbol rename and a TODO sweep, and do not want to retype either pattern.
Example
Recent search history:
1) \<OldType\>
2) TODO\s*:
Typing:
/\V<C-r>=histget('/', -2)<CR>
injects the older pattern directly into the prompt and executes it.
Tips
- Use
histget('/', -3)or lower to jump further back - Combine with
nandNto keep traversing after replay - Use
:history /when you need to inspect indices explicitly