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

How do I preview a function or tag definition without leaving my current position?

Answer

<C-w>}

Explanation

How it works

The <C-w>} command opens a preview window showing the tag definition of the word under your cursor. Unlike <C-]> which jumps to the tag and replaces your current view, the preview window appears as a small read-only split at the top of the screen, keeping your cursor exactly where it is.

The preview window is a special window type in Vim. Only one preview window can exist at a time -- if you press <C-w>} on a different identifier, the preview window updates to show the new definition.

Related commands:

  • :ptag tagname opens a specific tag in the preview window
  • :pclose or <C-w>z closes the preview window
  • :pedit filename opens any file in the preview window
  • <C-w>g} is like <C-w>} but uses :ptjump which lets you pick from multiple matches

Example

Suppose you are reading a file and encounter a function call parse_config():

result = parse_config(filepath)

Place your cursor on parse_config and press <C-w>}. A small preview window appears at the top showing the function definition:

+-- Preview: config.py -----------+
| def parse_config(path):         |
|     with open(path) as f:       |
|         return json.load(f)     |
+---------------------------------+
| result = parse_config(filepath) |
|   ^ cursor stays here           |
+---------------------------------+

This requires a tags file. Generate one with ctags -R . in your project root. When you are done previewing, press <C-w>z to close the preview window.

Next

How do you yank a single word into a named register?