Home » Questions » Computers [ Ask a new question ]

How do I edit an existing buffer in a new tab in vim?

How do I edit an existing buffer in a new tab in vim?

Suppose I have started vim like this:

Asked by: Guest | Views: 61
Total answers/comments: 4
Guest [Entry]

"You wish to open a buffer in a new tab ?

Split up the screen (Ctrl-W s), take up a window, and Ctrl-W T"
Guest [Entry]

"You can accomplish this by combining the tab command with the sb[uffer] command.

First you'll need to know the buffer id of the buffer you wish to open in a new tab. You can find this out with the ls command:

:ls
1 %a ""foo"" line 1
2 ""bar"" line 0

Once you have the id, you can easily open it in a new tab using:

:tab sb 2

The sb command normally opens the given buffer in a new split window, but the tab command causes it to open in a new tab, instead.

The tab command also allows you to specify where in the tab list the new tab should be created. For example, :0tab sb 2 would result in the new ‘bar’ tab appearing at the beginning of the list instead of after the current tab."
Guest [Entry]

"Just add some point which other guys didn't mention.

current window to new tab

If have multiple window, <C-W>T will move this window to new tab. However, this shortcut only for ""Window"", not ""buffer"". If prefer this style, :sp or <C-W>s to duplicate current buffer to one more window, then <C-W>T to move it to new tab.

4 keystrokes or 7 keystrokes.

current buffer to new tab

:tabe % to open new tab for current buffer.

7 keystrokes.

buffer to new tab

If use CtrlP plugin, also could use ""CtrlPBuffer"", then with <C-t> shortcut to open it with new tab page.
This style, easily to switch to different buffers.

With shortcut of ""CtrlPBuffer"", 4 keystrokes or more."
"Just add some point which other guys didn't mention.

current window to new tab

If have multiple window, <C-W>T will move this window to new tab. However, this shortcut only for ""Window"", not ""buffer"". If prefer this style, :sp or <C-W>s to duplicate current buffer to one more window, then <C-W>T to move it to new tab.

4 keystrokes or 7 keystrokes.

current buffer to new tab

:tabe % to open new tab for current buffer.

7 keystrokes.

buffer to new tab

If use CtrlP plugin, also could use ""CtrlPBuffer"", then with <C-t> shortcut to open it with new tab page.
This style, easily to switch to different buffers.

With shortcut of ""CtrlPBuffer"", 4 keystrokes or more."
Guest [Entry]

"If you are using fzf.vim plugin. You could list the buffers using :Buffers, then select the specific buffer with <C-j/k> (or filter by typing buffer number or file name), then use <C-t> to open the selected buffer in new tab.

I use below mapping to quickly list the buffers:

nnoremap <leader>b :Buffers<CR>"