Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
What is a "net tab"? –  eleven81 Nov 5 '09 at 15:33
    
Sorry for the typo and thanks to Idigas for correcting it. –  innaM Nov 5 '09 at 15:48

4 Answers 4

up vote 10 down vote accepted

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.

share|improve this answer
    
Yes, but I want to have tabs. –  innaM Nov 5 '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 5 '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 5 '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 5 '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 5 '09 at 17:08

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

share|improve this answer
    
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 5 '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. –  ldigas Nov 5 '09 at 15:37
    
Ah! You're right. I was misinterpreting the output of :ls. –  innaM Nov 5 '09 at 15:46
    
Also, ctrl-w V splits the window vertically. –  Shannon Nelson Nov 5 '09 at 20:33
1  
CTRL-w v is the correct command for splitting windows vertically –  JRM Nov 10 '12 at 1:59

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.

share|improve this answer
    
An similar approach from a previous edit, for posterity, is :bufdo execute "tabnew %". I think the new approach is a bit clearer. –  John Whitley May 30 '12 at 1:37
    
One caveat: if the starting buffer in vim isn't empty, this seems to open the last buffer twice. I'll post an update if I find a simple fix. –  John Whitley May 30 '12 at 1:52
5  
:tab sball seems to work better. –  ggustafsson Jan 20 '13 at 11:07

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
share|improve this answer
3  
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 2 '13 at 20:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.