tags:

views:

6362

answers:

7

I have looked at the ability to use tabs in vim (with :tabe, :tabnew, etc.) as a replacement for my current practice of having many files open in the same window in hidden buffers.

I would like every distinct file that I have open to always be in its own tab. However, there are some things that get in the way of this. How do I fix these:

1) When commands like gf and ^] jump to a location in another file, the file opens in a new buffer in the current tab. Is there a way to have all of these sorts of commands open the file in a new tab, or switch to the existing tab with the file if it is already open?

2) When switching buffers I can use :b <part of filename><tab> and it will complete the names of files in existing buffers. <part of filename> can even be the middle of a filename instead of the beginning. Is there an equivalent for switching tabs?

+1  A: 

Looking at :help tabs it doesn't look like vim wants to work the way you do...

Buffers are shared across tabs, so it doesn't seem possible to lock a given buffer to appear only on a certain tab.

It's a good idea, though.

You could probably get the effect you want by using a terminal that supports tabs, like multi-gnome-terminal, then running vim instances in each terminal tab. Not perfect, though...

Mike G.
+1  A: 
  • You can map commands that normally manipulate buffers to manipulate tabs, as I've done with gf in my .vimrc:

    map gf :tabe <cfile><CR>
    

    I'm sure you can do the same with [^

  • I don't think vim supports this for tabs (yet). I use gt and gT to move to the next and previous tabs, respectively. You can also use Ngt, where N is the tab number. One peeve I have is that, by default, the tab number is not displayed in the tab line. To fix this, I put a couple functions at the end of my .vimrc file (I didn't paste here because it's long and didn't format correctly).

Lucas Oman
+5  A: 

I ran into the same problem. I wanted tabs to work like buffers and I never quite manage to get them to. The solution that I finally settled on was to make buffers behave like tabs!

Check out the plugin called Mini Buffer Explorer, once installed and configured, you'll be able to work with buffers virtaully the same way as tabs without losing any functionality.

Dominic Dos Santos
+45  A: 

Stop, stop, stop.

This is not how vim's tabs are designed to be used. In fact, they're misnamed. A better name would be "viewport" or "layout", because that's what a tab is -- it's a different layout of windows of ALL of your existing buffers.

Trying to beat vim into 1 tab == 1 buffer is an exercise in futility. Vim doesn't know or care and it will not respect it on all commands -- in particular, anything that uses the quickfix buffer (:make, :grep, and :helpgrep are the ones that spring to mind) will happily ignore tabs and there's nothing you can do to stop that.

Instead:

  • :set hidden
    If you don't have this set already, then do so. It makes vim work like every other multiple-file editor on the planet. You can have edited buffers that aren't visible in a window somewhere.
  • Use :bn, :bp, :b #, :b name, and ctrl-6 to switch between buffers. I like ctrl-6 myself (alone it switches to the previously used buffer, or #ctrl-6 switches to buffer number #)
  • Use :ls to list buffers, or a plugin like MiniBufExpl or BufExplorer
Zathrus
Very nice suggestions, thanks!
Mike G.
Well summed up!
Masi
To me, this is a bug, not "user error". Searching around the web for 'vim tabs' indicates that that just about everyone else disagrees with you, or is unaware of the "real" way to use Vim tabs. Also, if tabs are really "layout" views, then why are default tab titles, the current file name? If I wanted multiple views of the same file, then all my tab titles would be same name (not very helpful?). So either way you look at it, the default tab implementation in Vim is flawed.
Casey
What else are you going to show in the tab title? It shows the current buffer name (not "file name"). You can change what displays in the tab title anyway.
Wahnfrieden
A: 

I use buffers like tabs, using the BufExplorer plugin and a few macros:

" CTRL+b opens the buffer list
map <C-b> <esc>:BufExplorer<cr>

" gz in command mode closes the current buffer
map gz :bdelete<cr>

" g[bB] in command mode switch to the next/prev. buffer
map gb :bnext<cr>
map gB :bprev<cr>

With BufExplorer you don't have a tab bar at the top, but on the other hand it saves space on your screen, plus you can have an infinite number of files/buffers open and the buffer list is searchable...

jkramer
+7  A: 

Bit late to the party here but surprised I didn't see the following in this list:

:tab sball - this opens a new tab for each open buffer

:he switchbuf - this controls buffer switching behaviour, try :se switchbuf=usetab,newtab. This should mean switching to the existing tab if the buffer is open, or creating a new one if not.

thrope