How do I open an existing buffer in a new tab page in Vim?
Answer
:tab sb N
Explanation
How it works
The :tab sb N command opens buffer number N in a brand new tab page. This is a combination of two concepts:
:sb N(short for:sbuffer N) opens buffer N in a new split window- The
:tabmodifier tells Vim to open the result in a new tab instead of a split
The :tab modifier is powerful because it can prefix many commands that would normally create a split. For example:
:tab sb 3opens buffer 3 in a new tab:tab splitopens the current buffer in a new tab:tab help optionsopens the help page in a new tab instead of a split
Example
Suppose you have several buffers open and want to give one its own tab:
- Run
:lsto see your buffers:1 %a "app.js" line 10 2 "routes.js" line 1 3 "models.js" line 1 - Type
:tab sb 3to openmodels.jsin its own tab. - Now you have two tab pages. Use
gtandgTto switch between them.
Note that this does not remove the buffer from any existing windows. The same buffer simply appears in an additional tab. Changes made in either location are reflected everywhere because they share the same underlying buffer.
This technique is useful when you want to temporarily focus on a single file in a full-screen view while keeping your split layout intact in another tab.