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

How do I open the file whose name is under the cursor in a new tab?

Answer

<C-w>gf

Explanation

<C-w>gf reads the filename under the cursor and opens it in a new tab page, keeping your current buffer untouched. It is the tab-based counterpart to gf (open in same window) and <C-w>f (open in a new split).

How it works

  • <C-w> — the window command prefix
  • gf — go to file under cursor
  • Combined: <C-w>gf opens the file in a new tab
  • If a line number follows the filename (e.g. config.lua:42), use <C-w>gF to open the file at that exact line
  • Vim searches the path option and the current working directory to locate the file

Example

Your init.lua contains:

require('plugins/telescope')

Place the cursor on plugins/telescope and press <C-w>gf. Vim opens plugins/telescope.lua (or similar, based on path and suffixesadd) in a new tab, so you can edit both files side by side across tabs.

Tips

  • Add set path+=** to your vimrc so Vim searches recursively under the current directory
  • set suffixesadd=.lua,.py,.js helps Vim auto-append extensions when the filename has none
  • Use <C-w>f when you prefer a horizontal split, or :vertical <C-w>f for a vertical split
  • After opening, switch back with gT (previous tab) and forward with gt (next tab)

Next

How do I run a search and replace only within a visually selected region?