A buffer in a window in a tab

From the vim docs.

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.

In brief

I believe that opening new buffers/files in the current tab or in a new tab, both are sensible workflows. Opening in a new tab certainly makes sense when you have many windows of the same buffer in the current tab. However, in almost all cases it will be more intuitive to close the current buffer with :bd (which also closes the tab/window unless there is a split with another buffer), instead of closing the current tab/window with :tabclose/:q (which will leave the buffer open if there is another active tab/window).

Advantages of tabs

  • Visual reminder that multiple buffers are open.
  • It is natural to open a new file in a new tab, as long as you make sure that you are actually closing the buffer when you are done working with that file instead of just the tab.
  • Say I have two windows open on the same buffer, one inspecting code in the beginning and one in the end. If I want to edit a second file, it would be more intuitive to open this buffer in a new tab, than to change each window in the current tab. The same logic can be applied when having one tab open.

Useful commands

vi <filename> opens an instance of vim and displays the file as a buffer in a window. Several windows can be used to view the same buffer (usually for different parts of the same file). :q and :x closes the current window. If the window is the last window of a tab, it will close the tab, but the file buffer will still be open if there is another active window. If the window is the last window of the vim-instance, it will close the remaining buffers and the vim-instance.

  • :e <filename> opens a file in a new buffer in the current window.
  • :ls shows all the open buffers.
  • :bn/:bp switches to the next/previous buffer. Map to gb and gB (nnoremap gb :bn<cr>).
  • :bd closes the current buffer. This will also close the current window/tab, unless there are no more open buffers. If you close the last buffer, you will remain in vim with an empty window. Don’t use :bw unless sure of what you’re doing.
  • :tabe <filename> opens a file in a new buffer in a new window in a new tab.
  • :tabc closes the current tab, you cannot close the last tab, i.e. you have to close the window or the buffer.
  • vi -p <file1> <file2> opens the files in separate buffers, windows and tabs.
  • gt/gT switches to the next/previous tab.
  • If using lots of buffers, you can add nnoremap <your-key-combo> :ls<cr>:b<space> to ~/.vimrc for faster navigation with buffer numbers.
  • :tab sball opens a new tab for each open buffer.
  • :set hidden allows to switch from buffers without saving them first.
  • :se switchbuf=usetab,newtab switching to the existing tab if the buffer is open, or creating a new one if not.
PhD candidate in stem cell bioengineering

Developmental biology researcher and data science educator