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

How do I open a file for reference in a dedicated preview window without disturbing my split layout?

Answer

:pedit

Explanation

Vim has a built-in preview window — a special window distinct from regular splits. It is designed for transient reference viewing: only one preview window can exist at a time, and commands like <C-w>z or :pclose close it without affecting the rest of your layout. Tag-based completion (:ptag) and certain plugins use it automatically, but you can open any file in it directly.

How it works

  • :pedit {file} — open {file} in the preview window (creates it if it does not exist)
  • :pedit % — open the current file itself in the preview window (useful to read a different part of the same buffer without a permanent split)
  • <C-w>P — jump the cursor into the preview window
  • <C-w>z or :pclose — close the preview window from any window
  • :ptag {tag} — look up a tag and display its definition in the preview window

Example

You are editing main.go and want to glance at utils.go without rearranging your splits:

:pedit utils.go
<C-w>P

Now browse utils.go in the preview window. When done:

<C-w>z

Your original split layout is completely unchanged.

Tips

  • Set previewheight (e.g., :set previewheight=12) to control how tall the preview window opens
  • The preview window is marked with [Preview] in the statusline
  • Use :ptjump {tag} to jump to a tag in the preview window and close any previously open one first
  • In insert mode, <C-w>} (or <C-]> in preview context) looks up the current word as a tag in the preview window

Next

How do I use capture groups in Vim substitutions to rearrange or swap matched text?