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

How do I open the current file in a preview window at a specific pattern?

Answer

:pedit +/TODO %

Explanation

When you need a second read-only view of the same file, opening more normal splits can disrupt your working layout. The preview window solves that: it is a dedicated, reusable window Vim treats specially. Using :pedit +/TODO % opens the current file (%) in that preview window and jumps directly to the first TODO match, which is great for reviewing markers while keeping your main editing cursor stable.

How it works

  • :pedit opens a file in Vim's preview window (or reuses an existing one)
  • +/TODO is a +cmd argument that runs after opening; here it searches for TODO
  • % expands to the current file path
  • Because this is the preview window, you can close it consistently with :pclose without affecting your main split arrangement

Example

:pedit +/TODO %

You keep coding in your current window, then jump into preview only when needed:

<C-w>P   " enter preview window
n        " next TODO in preview
<C-w>p   " return to previous window

Tips

  • Replace TODO with any pattern, for example FIXME or a function name.
  • Use :set previewheight=12 to make the preview window size predictable.
  • :pclose closes only the preview window, which keeps this workflow low-friction.

Next

How do I paste the unnamed register after transforming it to uppercase?