How do I open a file in a new tab in Vim?
Answer
:tabnew filename
Explanation
The :tabnew filename command opens a file in a new tab page in Vim. Tabs provide a way to organize multiple windows and buffers into separate views, similar to tabs in a web browser.
How it works
:tabnew filenameopensfilenamein a new tab:tabnewwithout a filename opens a new tab with an empty buffer:tabe filenameis a shorter alias (short for:tabedit)
Example
You are editing main.go and want to open README.md in a separate tab:
:tabnew README.md
Vim creates a new tab showing README.md. The tab bar appears at the top of the screen, listing both tabs.
Navigating between tabs
gt— go to the next tabgT— go to the previous tab3gt— go to tab number 3:tabn— next tab (same asgt):tabp— previous tab (same asgT):tabfirst— go to the first tab:tablast— go to the last tab
Tips
- Use
:tabcloseor:tabcto close the current tab - Use
:tabonlyor:taboto close all tabs except the current one - Use
<C-w>Tto move the current split window into its own new tab - Use
:tabsto list all open tabs and the windows they contain - Tabs in Vim are different from tabs in other editors — each Vim tab can contain multiple splits (windows), making them more like "layouts" or "workspaces"
- Use
:tabdo %s/old/new/gto run a substitution command across all open tabs - Many users prefer buffers over tabs for file switching, but tabs excel at organizing different tasks or contexts (e.g., one tab for source code, another for tests)