69

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?

Rook
  • 23,617
  • 32
  • 128
  • 213
innaM
  • 10,192
  • 5
  • 42
  • 52
  • Many of you are probably looking for `tabe %` which is mentioned in [liuyang1's answer](https://superuser.com/a/926567/442991) below. – Mateusz Piotrowski Apr 25 '17 at 13:45
  • 1
    @MateuszPiotrowski Like I mentioned in [this comment to that answer](https://superuser.com/questions/66179/how-do-i-edit-an-existing-buffer-in-a-new-tab-in-vim#comment2176230_926567), `:tabe %` doesn't work with buffers that have no valid filepath. `:tab sb` works everytime. – JoL May 28 '19 at 15:57

7 Answers7

63

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

Rook
  • 23,617
  • 32
  • 128
  • 213
  • Hmm. Not quite what I had in mind, but not bad for a start. I didn't know about `Ctrl-w T` yet. Of course, the first tab will still have two buffers that way. – innaM Nov 05 '09 at 15:34
  • No. After you split the screen into two windows, and open one of them in a new tab, it goes away from the first tab. It won't remain (at least it doesn't on my gvim72). As far as buffers go, they are not connected to windows/tabs ... they are more like memory where vim stores file contents. – Rook Nov 05 '09 at 15:37
  • Ah! You're right. I was misinterpreting the output of `:ls`. – innaM Nov 05 '09 at 15:46
  • Also, ctrl-w V splits the window vertically. – Shannon Nelson Nov 05 '09 at 20:33
  • 1
    CTRL-w v is the correct command for splitting windows vertically – JRM Nov 10 '12 at 01:59
41

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.

rkjnsn
  • 551
  • 4
  • 6
  • 4
    You don’t need the buffer number, give it an unambiguous part of the buffer name and VIM does the rest for you. – dessert Aug 15 '18 at 08:44
  • 1
    This was so the answer I was looking for and needed. I was dismayed when `:tab buffer part-of-name` did not open a new tab! But `:tab sb part-of-name` worked like a charm. Thank you!!!! – Sukima Jul 29 '19 at 02:44
  • This is not OP's exact question, but was exactly what I was looking for :) – jackcogdill Nov 02 '19 at 00:10
35

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 :b2

If you want to always have buffers loaded into their own tabs, check out this article.

DaveParillo
  • 14,505
  • 1
  • 39
  • 46
  • Yes, but I want to have tabs. – innaM Nov 05 '09 at 15:55
  • So you already have started vim in a way that you have your files in the vim client, not in a cmd / terminal shell? – DaveParillo Nov 05 '09 at 15:57
  • 1
    I'm not sure what you mean. I use the shell to start vim like described above and then I have a running vim. – innaM Nov 05 '09 at 16:13
  • Ah! I guess I never really understood that buffers aren't local to tabs. I always thought (without thinking really much) that each tab has its own buffer list. – innaM Nov 05 '09 at 16:42
  • On my machine, `vim` will launch an editor within the shell. To get the vim graphical user interface I have to use `gvim`. And you are correct - buffers are global to the vim application. – DaveParillo Nov 05 '09 at 17:08
  • Same here. vim will give me the "ungraphical" vim in my terminal window. – innaM Nov 05 '09 at 17:19
  • So in that case you'll need to switch between tabs. `gt` is the fastest, or you can tab switch the same way you switch between buffers - `:tabnext` (or `:tabn 2`), `:tabprev` – DaveParillo Nov 05 '09 at 17:51
  • oh man this gave me answer to all my questions. Really appreciated. – jav Nov 10 '20 at 16:31
30

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:

 :tab sball

It's well worth looking up the Vim help for :tab and :sball to see what's going on here.

John Whitley
  • 505
  • 4
  • 10
7

1. Open two files in Vim.

$ vim foo bar

2. Check the numbers of buffers.

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

3. Chain two commands: tabnew to open a new tab and b <buffer_number> to load the desired buffer in the tab.

:tabnew | b 2
Shamaoke
  • 516
  • 7
  • 13
  • 9
    The problem with step 3 is that it first creates a new tab with an empty buffer, and then opens buffer 2, resulting in an extra untitled buffer in the buffer list. Better to use `:tab sb 2` – rkjnsn Apr 02 '13 at 20:26
  • 1
    @rkjnsn you should post that as an answer - it best answers the question 'how do I edit an existing buffer in a new tab in vim?' – JonnyRaa Mar 19 '15 at 11:32
  • 1
    @JonnyLeeds done. – rkjnsn Mar 20 '15 at 18:33
4

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.

liuyang1
  • 141
  • 3
  • `:tabe %` is what I've been looking for for a long time, thanks! – pevik Dec 05 '17 at 16:47
  • No, `:tabe %` doesn't really open a new tab for current buffer. What happens is that `%` gets expanded to the filepath of the current buffer and `:tabe` opens that path. Vim will see you're trying to open a file you already have open and will reuse the buffer you have. This means that this doesn't work with buffers that have no filepath. If you open up a new file with `:new` and you haven't saved it, you can't put it on a new tab with this. The real command you need is what rkjnsn put in their answer: `:tab sb %` or shorter: `:tab sb` – JoL May 28 '19 at 15:49
0

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>
Brij
  • 101
  • 2