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
:peditopens a file in Vim's preview window (or reuses an existing one)+/TODOis a+cmdargument that runs after opening; here it searches forTODO%expands to the current file path- Because this is the preview window, you can close it consistently with
:pclosewithout 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
TODOwith any pattern, for exampleFIXMEor a function name. - Use
:set previewheight=12to make the preview window size predictable. :pclosecloses only the preview window, which keeps this workflow low-friction.