How do I browse and reuse my previous search patterns in Vim?
Answer
q/
Explanation
How it works
Vim keeps a history of all your search patterns. Pressing q/ in normal mode opens a special search history window at the bottom of the screen, listing every search you have performed in the current session (and across sessions if you have viminfo configured).
In this window you can:
- Scroll through previous searches with
jandk - Press Enter on any line to execute that search
- Edit a search pattern before executing it (it is a regular buffer)
- Use
/within the window to search your search history - Press
<C-c>or:qto close without searching
For a quicker approach, while in the / search prompt, you can press the Up and Down arrow keys to cycle through previous searches one at a time. But q/ gives you the full list at once.
The counterpart q? opens the history for backward searches.
Example
Suppose you searched for several patterns during a debugging session:
/error_code
/def handle_request
/TODO
/import.*logging
Later you want to re-run one of those searches. Press q/ and you see:
import.*logging
TODO
def handle_request
error_code
The most recent search is at the bottom. Navigate to def handle_request and press Enter to jump to that pattern.
You can also modify the pattern before executing. Navigate to a line, edit it (for example, change handle_request to handle_response), then press Enter to search for the modified pattern.
This is similar to q: which opens command-line history, and both are essential tools for avoiding repetitive typing.