How do I reuse the last search pattern without retyping it in Vim?
Answer
//
Explanation
In Vim, pressing // (two forward slashes) in Normal mode repeats the last search pattern. This saves you from retyping a complex regex when you want to search again, perhaps with a different offset or flag.
How it works
/— enters search mode- leaving the pattern empty — Vim reuses the last search pattern
You can combine the empty pattern with search offsets to land at a specific position relative to the match:
//— jump to next occurrence of last pattern (same asn)//e— jump to the end of the next match//b+2— jump 2 characters after the beginning of the next match//e-1— jump 1 character before the end of the next match
Example
After searching for /function, you can run:
//e
This jumps to the end of the next match of function, placing the cursor on the n. Useful for an a (append) command right after the match.
Before: |function myFunc() {
After: function myFun|c() { (cursor at end of 'function')
Tips
//eis handy combined withato append text immediately after a matched word- Use
?analogously:??esearches backward with the last pattern and lands at end of match - The last search register
@/stores the pattern; you can inspect it with:echo @/