Using Vim's tabs like buffers
Asked Answered
C

11

390

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?

Cece answered 19/9, 2008 at 14:42 Comment(0)
S
695

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.
Scandium answered 19/9, 2008 at 16:44 Comment(6)
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.Premillennial
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.Krimmer
Keep in mind that CTRL-^ may be easier to reach than CTRL-6 on non-US keyboards. On German keyboards, ^ is located left of the 1 – very convenient. :h CTRL-^: Mostly the ^ character is positioned on the 6 key, pressing CTRL and 6 then gets you what we call CTRL-^. But on some non-US keyboards CTRL-^ is produced in another way.Cornell
+1 for "Stop, stop, stop". Vim is as much a mentality as it is a tool. It's important to teach new users the mentality, so they can use the tool to its full potential.Chlorella
Drew Neil's vimcast on How to use tabs does an excellent comparison of tabs in Textmate vs tab pages in VimGravure
@Krimmer For tab titles, numbering would work, or custom naming.Sparoid
M
286

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.

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

Monopteros answered 13/8, 2010 at 11:36 Comment(3)
+1 for switchbuf - set switchbuf=useopen is great to avoid the annoying behavior of :make, :vimgrep and similar commands that changes the buffers in the window layout even when the buffer was already displayed.Surprising
why no fit me? after set :se switchbuf=usetab,newtab,when a use bn to switch buffer,it still open the buffer file in current tab instead of switching to the existing tab? Is there something wrong?Ahola
switchbuf only works wtih buffer split commands (:sb) and quickfix commands (:cc). Source: vim documentation.Unimpeachable
M
197

Vim :help window explains the confusion "tabs vs buffers" pretty well.

A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.

Opening multiple files is achieved in vim with buffers. In other editors (e.g. notepad++) this is done with tabs, so the name tab in vim maybe misleading.

Windows are for the purpose of splitting the workspace and displaying multiple files (buffers) together on one screen. In other editors this could be achieved by opening multiple GUI windows and rearranging them on the desktop.

Finally in this analogy vim's tab pages would correspond to multiple desktops, that is different rearrangements of windows.

As vim help: tab-page explains a tab page can be used, when one wants to temporarily edit a file, but does not want to change anything in the current layout of windows and buffers. In such a case another tab page can be used just for the purpose of editing that particular file.

Of course you have to remember that displaying the same file in many tab pages or windows would result in displaying the same working copy (buffer).

Manolete answered 23/1, 2012 at 15:5 Comment(4)
This is actually an awesome answer, but took a couple of reads to make sense. buffers = tabs, windows = gui windows, tab pages = desktops make the best sense so far that I've found for someone who isn't familiar with the conceptsGravure
I tend to think of them as looking through a window into a room - buffer = object, window = pane, and tab = window. I can move around the room and look in from different windows (tabs). Different windows can have different number, arrangement and size of panes. The same object can be simultaneously viewed from different panes in different windows.Endodermis
If you are familiar with the Linux Workspaces and Windows paradigm, then you will see the heritage of vim usage. In the beginning there was just a window. Window managers then allowed you to split the windows into panes (tiling WM) or into overlapping panes (,floating WM) but they still referred to each pane as a window. Now add the concept of a workspace where pane can be shown in multiple workspaces. So, in this paradigm, buffer = app, window = window and tab = workspace.Endodermis
Another way kd thinking about it, thst others (like NotePad++) are still using multiple buffers, the tabs are just ways to present that to the user. What really is the difference is, that Vim's :ls output is not always visible. (See BufExplorer and MiniBufExpl mentioned above)Beneficent
I
58

Contrary to some of the other answers here, I say that you can use tabs however you want. vim was designed to be versatile and customizable, rather than forcing you to work according to predefined parameters. We all know how us programmers love to impose our "ethics" on everyone else, so this achievement is certainly a primary feature.

<C-w>gf is the tab equivalent of buffers' gf command. <C-PageUp> and <C-PageDown> will switch between tabs. (In Byobu, these two commands never work for me, but they work outside of Byobu/tmux. Alternatives are gt and gT.) <C-w>T will move the current window to a new tab page.

If you'd prefer that vim use an existing tab if possible, rather than creating a duplicate tab, add :set switchbuf=usetab to your .vimrc file. You can add newtab to the list (:set switchbuf=usetab,newtab) to force QuickFix commands that display compile errors to open in separate tabs. I prefer split instead, which opens the compile errors in a split window.

If you have mouse support enabled with :set mouse=a, you can interact with the tabs by clicking on them. There's also a + button by default that will create a new tab.

For the documentation on tabs, type :help tab-page in normal mode. (After you do that, you can practice moving a window to a tab using <C-w>T.) There's a long list of commands. Some of the window commands have to do with tabs, so you might want to look at that documentation as well via :help windows.

Addition: 2013-12-19

To open multiple files in vim with each file in a separate tab, use vim -p file1 file2 .... If you're like me and always forget to add -p, you can add it at the end, as vim follows the normal command line option parsing rules. Alternatively, you can add a bash alias mapping vim to vim -p.

Irascible answered 11/11, 2012 at 5:27 Comment(3)
+1 for vim -p however be aware that only up to 10 tabs will be display by default. Extra files are loaded into buffers, but not displayed in their own tab.Harmaning
@IanB: how to I overcome the default setting of limiting it to 10 tabs?Cooke
@Cooke see tabpagemax settingHarmaning
C
14

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.

Catalyze answered 19/9, 2008 at 15:12 Comment(0)
G
11

This is an answer for those not familiar with Vim and coming from other text editors (in my case Sublime Text).

I read through all these answers and it still wasn't clear. If you read through them enough things begin to make sense, but it took me hours of going back and forth between questions.

The first thing is, as others have explained:

Tab Pages, sound a lot like tabs, they act like tabs and look a lot like tabs in most other GUI editors, but they're not. I think it's an a bad mental model that was built on in Vim, which unfortunately clouds the extra power that you have within a tab page.

The first description that I understood was from @crenate's answer is that they are the equivalent to multiple desktops. When seen in that regard you'd only ever have a couple of desktops open but have lots of GUI windows open within each one.

I would say they are similar to in other editors/browsers:

  1. Tab groupings
  2. Sublime Text workspaces (i.e. a list of the open files that you have in a project)

When you see them like that you realise the power of them that you can easily group sets of files (buffers) together e.g. your CSS files, your HTML files and your JS files in different tab pages. Which is actually pretty awesome.

Other descriptions that I find confusing

Viewport

This makes no sense to me. A viewport which although it does have a defined dictionary term, I've only heard referring to Vim windows in the :help window doc. Viewport is not a term I've ever heard with regards to editors like Sublime Text, Visual Studio, Atom, Notepad++. In fact I'd never heard about it for Vim until I started to try using tab pages.

If you view tab pages like multiple desktops, then referring to a desktop as a single window seems odd.

Workspaces

This possibly makes more sense, the dictionary definition is:

A memory storage facility for temporary use.

So it's like a place where you store a group of buffers.

It didn't initially sound like Sublime Text's concept of a workspace which is a list of all the files that you have open in your project:

the sublime-workspace file, which contains user specific data, such as the open files and the modifications to each.

However thinking about it more, this does actually agree. If you regard a Vim tab page like a Sublime Text project, then it would seem odd to have just one file open in each project and keep switching between projects. Hence why using a tab page to have open only one file is odd.

Collection of windows

The :help window refers to tab pages this way. Plus numerous other answers use the same concept. However until you get your head around what a vim window is, then that's not much use, like building a castle on sand.

As I referred to above, a vim window is the same as a viewport and quiet excellently explained in this linux.com article:

A really useful feature in Vim is the ability to split the viewable area between one or more files, or just to split the window to view two bits of the same file more easily. The Vim documentation refers to this as a viewport or window, interchangeably.

You may already be familiar with this feature if you've ever used Vim's help feature by using :help topic or pressing the F1 key. When you enter help, Vim splits the viewport and opens the help documentation in the top viewport, leaving your document open in the bottom viewport.

I find it odd that a tab page is referred to as a collection of windows instead of a collection of buffers. But I guess you can have two separate tab pages open each with multiple windows all pointing at the same buffer, at least that's what I understand so far.

Gravure answered 30/3, 2017 at 18:15 Comment(1)
Finally after a solid year of full-time Vim usage, I came across a case where I wanted to use more than one tab page. Currently buffers work perfectly for me as I would use tabs in other editors. But having a large monitor I was comparing a whole bunch of 3-4 different buffers. Then a colleague wanted me to search for some code elsewhere in the code base. Rather than lose my set of comparison windows, I could just create a new tab page which then allows me to do a separate search as I would when just using Vim normally and then I could close the tab page once done and return to my initial setGravure
S
5
  • 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).

Smoking answered 19/9, 2008 at 14:53 Comment(1)
<c-w>gf will open the filename under the cursor in a new tabLaveen
W
5

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...

Wigfall answered 19/9, 2008 at 22:46 Comment(0)
D
4

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...

Delinquent answered 19/9, 2008 at 14:50 Comment(4)
I tried that, the problem with that, besides making it difficult to navigate between tabs, is that yanked text can't be shared across terminal tabs, as they are essentially two different terminals/shellsCommunicant
@Communicant : Try the YankRing vim plugin. It saves yanked text in a file, so it can be shared accross vim instances.Fieldsman
@Pixelastic I found a suitable solution here for yanking to system clipboard. Also, Vim works best in one instance. I stopped making it do what I wanted it, and learned how to properly use vim :-) https://mcmap.net/q/55207/-can-vim-use-the-system-clipboard-s-by-defaultCommunicant
@Communicant : Thanks, it didn't occurred to me that using the system clipboard means sharing it across vim instances as well :)Fieldsman
G
2

If you want buffers to work like tabs, check out the tabline plugin.

That uses a single window, and adds a line on the top to simulate the tabs (just showing the list of buffers). This came out a long time ago when tabs were only supported in GVim but not in the command line vim. Since it is only operating with buffers, everything integrates well with the rest of vim.

Gustatory answered 27/6, 2013 at 21:12 Comment(0)
H
0

relevant: Vim: can global marks switch tabs instead of the file in the current tab?

maybe useful to solve a little part of OP's problem:

    nno \ <Cmd>call To_global_mark()<cr>
    fun! To_global_mark()
       -tab drop /tmp/useless.md
        exe 'normal! `' .. toupper(input("To global mark: "))
    endf
Hierology answered 19/4, 2022 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.