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

How do I peek at a function definition without leaving my current window?

Answer

:ptag {identifier}

Explanation

:ptag {identifier} opens a small preview window showing the definition of the given tag, while keeping your cursor in the original window. This lets you inspect a function signature or variable declaration without disturbing your current position in the file — ideal when you want a quick reference without losing your context.

How it works

  • :ptag — like :tag, but opens the result in the preview window instead of the current window
  • {identifier} — the tag name to look up (must exist in your tags file)
  • The preview window is a fixed, single special window (there can only be one at a time)
  • Close it with :pclose or <C-w>z

For cursor-based lookup (the identifier under the cursor), use the normal-mode command:

<C-w>}

This is the quickest way to trigger a preview for the word under the cursor.

Example

With cursor on getUserById in your code:

<C-w>}

Vim opens a preview window at the top showing the function definition from your tags file. Your cursor stays in the original buffer. When done:

:pclose

Tips

  • <C-w>P (uppercase P) — jump your cursor into the preview window so you can scroll or copy from it
  • :pclose / <C-w>z — close the preview window from any window
  • Set previewheight to control the preview window size: :set previewheight=12
  • Requires a tags file — generate with ctags -R . in your project root
  • In Neovim with LSP configured, :lua vim.lsp.buf.hover() offers a similar experience without tags

Next

How do I delete text from the cursor to the next occurrence of a pattern?