Skip to content

Vim - Working With Multiple Things

Vim - Working With Multiple Files, Buffers, Windows, and Tabs

Section titled “Vim - Working With Multiple Files, Buffers, Windows, and Tabs”

Vim excels at managing multiple contexts simultaneously. This chapter explores how to work efficiently with multiple files, buffers (in-memory copies of files), windows (splits of the Vim screen), and tabs (collections of windows).

  • Editing multiple files
  • Managing multiple buffers
  • Using multiple tabs
  • Working with multiple windows (splits)

You can open multiple files when starting Vim or open additional files during a session.

To open multiple files from the command line (each will typically open in its own buffer):

$ vim file1.txt file2.js file3.md

Within Vim, to open another file in the current window (replacing the current buffer in that window):

CommandDescription
:edit Load <filename> into the current window. Shorthand: :e <filename>.
:edit Press Tab after :e to see file completions from the current directory.
:find Search for <filename> in Vim’s path and open it. Useful for project files. See :help 'path'.

If the current file has unsaved changes, Vim will prevent you from switching unless you save (:w) or force the command (e.g., :e! <filename>).

A buffer is an in-memory copy of a file being edited. Every file you open is loaded into a buffer. You can have many buffers open but typically only one or a few are visible in windows at any time.

CommandDescription
:ls or :buffersList all open buffers with their numbers and status.
:buffer <N_or_name>Switch to buffer number N or buffer with a partial name. Shorthand: :b <N_or_name>.
:bnext or :bnGo to the next buffer in the list.
:bprevious or :bpGo to the previous buffer in the list.
:bfirst or :bfGo to the first buffer in the list.
:blast or :blGo to the last buffer in the list.
:badd Add <filename> to the buffer list without opening it in a window.
:bdelete <N_or_name> or :bd <N_or_name>Unload buffer N or by name. If it has changes, you’ll be warned. Use :bd! to force.

Example: If :ls shows buffer 3 is notes.txt: :b 3 or :b notes would switch the current window to display notes.txt.

Output of :ls might look like:

1 %a "file1.txt" line 1
2 # "file2.js" line 0
3 "file3.md" line 0

% indicates the current buffer, # the alternate buffer.

Tabs (or tab pages) in Vim are collections of one or more windows. They allow you to manage different workspaces or sets of related files.

CommandDescription
:tabnew Open <filename> in a new tab. If no filename, opens an empty buffer. Shorthand: :tabe <filename>.
:tabclose or :tabcClose the current tab. If it’s the last tab, Vim may quit (depending on settings).
:tabnext or :tabn or gtGo to the next tab page.
:tabprevious or :tabp or gTGo to the previous tab page.
:tabfirst or :tabfirGo to the first tab page.
:tablast or :tablGo to the last tab page.
NgtGo to tab page number N (e.g., 3gt goes to the third tab).
:tabsList all tab pages and the windows they contain.

Visually, tabs are often shown at the top of the Vim window if you’re using a GUI version or a terminal that supports it, or if you have plugins like Airline.

Windows (often called splits) allow you to view and edit multiple buffers simultaneously, or different parts of the same buffer, by dividing the Vim screen.

CommandDescription
:split or :sp Split the current window horizontally and open <filename> in the new window. If no filename, duplicates the current buffer.
:vsplit or :vs Split the current window vertically and open <filename>. If no filename, duplicates the current buffer.
:new Create a new horizontal split with <filename> (or an empty buffer).
:vnew Create a new vertical split with <filename> (or an empty buffer).
Ctrl-w s or Ctrl-w Ctrl-sNormal mode: Split current window horizontally (like :split).
Ctrl-w v or Ctrl-w Ctrl-vNormal mode: Split current window vertically (like :vsplit).
Ctrl-w q or :quit or :qClose the current window. Shorthand: :clo or Ctrl-w c.
Ctrl-w o or :onlyMake the current window the only one on the screen, closing all others in the tab.
Ctrl-w <h,j,k,l> or Ctrl-w <ArrowKey>Move cursor to the window in the specified direction (left, down, up, right).
Ctrl-w w or Ctrl-w Ctrl-wCycle through open windows.

Example: If you are editing main.c: :sp utils.h - will split the screen horizontally, showing main.c in one window and utils.h in the other.

Resizing windows: Ctrl-w + (increase height), Ctrl-w - (decrease height), Ctrl-w > (increase width), Ctrl-w < (decrease width), Ctrl-w = (equalize sizes).