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

How do I run vimgrep across a project using whatever pattern is currently in the search register?

Answer

:execute 'vimgrep /' . @/ . '/gj **/*'

Explanation

If you already refined a search interactively with / or ?, retyping that pattern for project-wide grep is repetitive and error-prone. This command reuses the current search register (@/) and injects it directly into :vimgrep, so your global search stays aligned with what you just validated in-buffer. It is a fast bridge from local exploration to project-wide impact analysis.

How it works

  • @/ holds the last search pattern exactly as Vim is using it
  • :execute ... builds an Ex command string dynamically
  • 'vimgrep /' . @/ . '/gj **/*' becomes a full :vimgrep call
  • g captures all matches per file; j avoids jumping to the first match during list construction
  • Results land in quickfix, ready for :copen, :cnext, :cdo, and related workflows

This is especially powerful when your search pattern is complex (word boundaries, groups, lookarounds) and you do not want to manually escape it again.

Example

First, refine a pattern in the current buffer:

/\<FeatureFlag\>\s*:\s*true

Then project-wide:

:execute 'vimgrep /' . @/ . '/gj **/*'
:copen

Now quickfix contains every matching occurrence across the project tree.

Tips

  • Run :set noignorecase or pattern flags before building @/ if case behavior matters
  • Scope files tighter than **/* when possible (for example **/*.ts) for speed
  • Pair with :cdo for repeatable fix-ups once matches are confirmed

Next

How do I search files on 'path' and open the first match in the preview window instead of replacing my current buffer?