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

How do I preview a tag definition in a preview window using :ptag?

Answer

:ptag function_name

Explanation

The :ptag command opens a tag definition in a small preview window at the top of the screen, letting you read the definition without losing your place in the current buffer. This is invaluable when you need to quickly check a function signature or type definition while editing.

How it works

  • :ptag {name} opens the tag {name} in the preview window
  • The preview window stays open and does not take focus
  • Use :pclose or <C-w>z to close the preview window
  • :ptjump {name} works like :ptag but shows a selector if multiple tags match
  • :ppop pops the preview window tag stack, going back to the previous tag

Example

While editing a file that calls calculate_total:

:ptag calculate_total

A small window appears at the top showing the definition:

+-- Preview Window -------------------------+
| def calculate_total(items, tax_rate):      |
|     subtotal = sum(i.price for i in items) |
|     return subtotal * (1 + tax_rate)       |
+-------------------------------------------+
| ... your current editing buffer ...        |
|                                            |
+-------------------------------------------+

Tips

  • Combine with <C-w>} to preview the tag under the cursor without typing the name
  • Set previewheight to control the preview window size: :set previewheight=8
  • Use :pedit filename to open any file in the preview window, not just tags
  • The preview window is special: only one can exist at a time, and it has previewwindow set

Next

How do I return to normal mode from absolutely any mode in Vim?