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

How do I close a preview window from any other window without having to jump to it first?

Answer

<C-w>z

Explanation

Pressing <C-w>z closes the preview window from any window in the current tab page. You do not need to jump into the preview window first — the command works wherever your cursor is.

How it works

Vim's preview window is a special split opened by commands like <C-w>} (preview tag under cursor), :ptag, or :pedit. There is only ever one preview window per tab.

  • <C-w>z — close the preview window from any window (same as :pclose)
  • <C-w>P — jump your cursor into the preview window (to edit or close it manually)

Without <C-w>z, you would need to <C-w>P to focus the preview window, then :q to close it — two moves instead of one.

Example

" Open the tag definition for the word under cursor in a preview window
<C-w>}

" ... read the preview, continue editing in your main window ...

" Close the preview window without leaving your current split
<C-w>z

Tips

  • :pclose is the Ex command equivalent of <C-w>z.
  • If no preview window is open, <C-w>z does nothing.
  • You can map it for convenience: nnoremap <leader>pc <C-w>z.

Next

How do I get just the filename without its path or extension to use in a command?