Suppose I have started vim like this:
vim foo bar
Now I decide that I want each of those files in its own tab. Is there a way to do that without exiting vim and adding the
-p option to my command line?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
When you start vim like that, you don't get a vim client, the text editor is using the terminal or cmd prompt - the two files are in two different buffers. Use
:ls to list the buffers:
:ls 1 %a "foo" line 6 2 "bar" line 0
The %a is the active buffer. You can use
:b2 to switch to buffer 2 or use
:bn to cycle to the next or
:bp for previous. I prefer (CTRL-W v) to split windows vertically, rather than (CTRL-W s), which splits horizontally.
If you have 2 files loaded & no tabs (yet), you can,
:tabnew and in the new tab type
If you want to always have buffers loaded into their own tabs, check out this article.
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 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
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.
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.
A better way to accomplish what OP asked for is this:
:bufdo tab split
This will open each buffer into a tab of its own, no matter how many there are. If you use this much, it's easy to make into a mapping in your .vimrc. Combined with something like this little vim plugin the following will open every item from
:grep (or :Ack) in a tab of its own:
:grep foo :QuickFixOpenAll :bufdo tab split
Of course, when resorting to a plugin it would be easy enough to modify it to open the quickfix list contents in directly into tabs.
UPDATE: I've really got to give a shout-out to ggustafsson's comment below. It's far and away the best answer of the lot and beautifully illustrates Vim's tendency towards compositional behavior. The suggestion is:
Just add some point which other guys didn't mention.
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,
<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.
:tabe % to open new tab for current buffer.
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.
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>