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

How do I run a literal project-wide vimgrep for the word under my cursor?

Answer

:vimgrep /\V<C-r><C-w>/gj **/*

Explanation

When you need a project-wide search for the exact word under your cursor, this pattern avoids regex surprises and immediately populates quickfix. It is especially useful when symbol names contain punctuation that would otherwise need escaping. The result is a reliable search command you can repeat quickly while refactoring.

How it works

  • :vimgrep searches files and writes all matches to the quickfix list
  • /.../ is the search pattern delimiter
  • \V switches Vim regex to very nomagic mode, so most characters are treated literally
  • <C-r><C-w> inserts the current word under the cursor into the command-line
  • g finds all matches per file (not just the first)
  • j does not jump to the first result immediately
  • **/* searches recursively from the current working directory

Example

Suppose the cursor is on api.v2_handler and you want exact matches across a repo:

:vimgrep /\V<C-r><C-w>/gj **/*
:copen

Quickfix is populated with every literal occurrence, ready for navigation with :cnext and :cprev.

Tips

  • Narrow file scope for speed: **/*.go or src/**/*
  • Follow with :cdo or :cfdo for controlled multi-file edits
  • If your cursor is on only part of a symbol, visually select text first and insert with <C-r>" on the command-line

Next

How do I launch GDB inside Vim using the built-in termdebug plugin without preloading it?