Buffers, windows and tabs

Vim · Practical · 12 min

Working on several files at once is essential, and Vim's model differs from the tabs-per-file model of other editors.

Vim separates three ideas:

  • A buffer is the in-memory text of a file. Every file you open stays loaded as a buffer.
  • A window is a viewport onto a buffer. Several windows can show the same buffer.
  • A tab page is a collection of windows, i.e. a layout.

Buffers: :e path opens a file, :ls lists buffers, :b 3 or :b part-of-name (Tab completes) switches, :bn/:bp cycle, :bd unloads one. Ctrl-^ toggles between the current and the previous buffer. With set hidden (default in Neovim, but not in Vim) you can switch away from a modified buffer without saving, so most people add it to their vimrc.

Windows: :split / :vsplit (or Ctrl-w s / Ctrl-w v) split the screen, Ctrl-w h/j/k/l move between windows, Ctrl-w o keeps only the current one, Ctrl-w = equalises sizes, :q closes a window (not the buffer).

Tabs: :tabnew, gt / gT to cycle, :tabclose. Use tabs for different layouts (say, code in one, tests and logs in another), not one tab per file; buffers already give you every open file.

:wa writes all modified buffers and :qa quits everything.

Example

:e src/app.py     " open a file in the current window
:ls               " list buffers
:b app            " switch to the buffer matching 'app'
Ctrl-^            " toggle with the previous buffer
:vsplit tests.py  " vertical split with another file
Ctrl-w l          " move to the window on the right
Ctrl-w o          " close all other windows
:tabnew           " new tab page (a new layout)
:wa               " write all changed buffers

Remember

  • Buffer = file in memory, window = view, tab = layout
  • :ls, :b name and Ctrl-^ navigate buffers
  • Ctrl-w + h/j/k/l moves between split windows

Watch out for

  • Using one tab per file and closing windows to 'close files' - the buffer stays loaded; use :bd to drop it.

Check yourself

  1. You close a split window with :q. What happens to the file shown in it?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/windows.txt.html