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

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

Answer

:psearch /pattern/

Explanation

When you need quick context from another file but do not want to disturb your current editing window, :psearch gives you a clean workflow. It searches files listed in your 'path' option, then opens a match in the dedicated preview window. This keeps your main window stable while still letting you inspect definitions, constants, or nearby code.

How it works

  • :psearch /pattern/ searches for pattern across files in 'path'
  • Instead of jumping the current window, Vim opens the result in the preview window
  • The preview window is special: it is meant for temporary reference content
  • You can return to your main window immediately and continue editing without buffer churn

This is especially effective when paired with project-aware 'path' settings so lookup targets are found quickly.

Example

Suppose you are editing service.py and need to inspect where BuildConfig appears in included source paths. Run:

:psearch /BuildConfig/

Your current window stays on service.py, while a preview window opens with a hit from another file:

[current window] service.py (unchanged)
[preview window] config/build.py at first BuildConfig match

You can examine context, then close preview when done:

:pclose

Tips

  • Ensure 'path' includes relevant project directories (:set path+=src/**)
  • Use <C-w>P to jump into the preview window only when needed
  • Prefer :psearch over ad-hoc split jumps when you want minimal layout disruption

Next

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