vimtricks.wiki Concise Vim tricks, one at a time.

How do I run vimgrep across files using my last search pattern without retyping it?

Answer

:vimgrep // **/*.txt

Explanation

Using // (an empty pattern) in :vimgrep tells Vim to reuse the last search pattern. This pairs naturally with /pattern or * (search word under cursor) — once you have a pattern active, you can immediately project it across all matching files without retyping it.

How it works

  • /keyword<CR> — search for keyword (sets the last pattern)
  • :vimgrep // **/*.go — run vimgrep on all .go files using that same pattern
  • :copen — open the quickfix list with all matches

The // form works identically to writing the full /pattern/ form, but saves you from retyping and avoids regex escaping mistakes. It works with any previous search, including those set by *, #, :s, or an explicit /.

Example

Search for the word under the cursor:

* " searches for the current word (sets last pattern)
:vimgrep // **/*.vim " finds all occurrences across .vim files
:copen   " browse results in the quickfix window

Or after a substitution that set the pattern:

:s/OldName/NewName/g  " pattern 'OldName' is now the last pattern
:vimgrep // **/*.go   " find any remaining 'OldName' across all .go files

Tips

  • Combine with :cdo s///g to do a project-wide search-and-replace without a plugin
  • Add the j flag (:vimgrep //j **/*.go) to populate the quickfix list without jumping to the first match
  • :lv[imgrep] // **/*.go uses the location list (window-local) instead of the global quickfix list

Next

How do I programmatically set a register's content in Vimscript to pre-load a macro?