It is not unusual to need to edit multiple files at the same time.  You could open these in separate terminals but it can be easier to reference multiple files within a single Vim session.  Vim provides a few way to do this.

Using Buffers

One way to manage multiple files is through buffers.   Buffers represent a file open that is open for editing. To open multiple files with Vim simply run:

vim file1 file2 file3

Each of these files is opened in its own buffer.  The first file will be displayed whilst the others are loaded into buffers.  You can see which buffers we have available by typing:

:buffers
:buffers
  1 %a  "file1"             line 1
  2     "file2"             line 0
  3     "file3"             line 0
Press ENTER or type command to continue

To change to the next file type :bn.

You can switch to a  buffer by number, or by name, by typing b followed by the number or name.  You don't have to type the entire name of the file you wish to switch to, just enough to be unique.

Here is a list of the most useful command when working with manage buffers:

  • :buffers: List available buffers
  • :ls: Same as above
  • :bn: Switch to next buffer
  • :bp: Switch to previous buffer
  • :bfirst: Switch to first buffer
  • :blast: Switch to last buffer
  • :bdelete: Delete the current buffer
  • :badd: Open a new buffer with the filename that follows
  • :e: Edit another file in a new buffer and switch to it.
Using Windows

Another method you can use to manage multiple files is to use windows (sometimes referred to as views).  This allows you to split the current editing area into different windows so that you can view multiple buffers at the same time.

To split the current workspace into separate windows, type :split or :sp.   This opens a new window above the current one and changes focus to that window.  Change the buffer shown in the new window by using the buffer commands as listed above.

The below outlines commands that are used to create and manage windows:

  • :sp: Split the current window in two.  The same buffer will be shown in each window initially.   Precede the "sp" with a number to set the new window height.
  • :vs: Split the current window vertically.  The same buffer will be shown in each window initially.   Precede the "vs" with a number to set the new window width.
  • CTRL-ww: Change focus to the next window
  • CTRL-w(movement): Change focus to the window in the direction (h,j,k,l) indicated
  • CTRL-wc: Close the current window
  • CTRL-w+: Increase size of current window
  • CTRL-w-: Decrease size of current window
  • CTRL-w=: Set all windows to equal size
  • #CTRL-w_: Sets the height to the size indicated by the preceding "#"
  • :only: Close all windows but the current one
  • CTRL-wn: Opens a new window with a new buffer
Using Tabs

The last method for managing multiple documents within is tabs.  Note that within Vim, tabs can contain windows, but not the other  way around.

You manage the window layout of each tab separately.  To create tabs use the :tabnew command to open a new tab.

Ways to manage tabs are:

  • :tabnew: Open new tab
  • :tabclose: Close current tab
  • :tabn: Switch to next tab
  • gt: Switch to next tab
  • :tabp: Switch to previous tab
  • gT: Switch to previous tab
  • :tab ball: Open all buffers in individual tabs
  • :tabs: List all available tabs

Lastly a quick way to find out the filename you're currently viewing is to type:

  • CTRL-g: Displays current file name
Summary

Vim is an incredibly versatile editor, with a lots of features such as those just highlighted.  Many of these are unknown, but hopefully the above has introduced a new method for handling multiple files within a single Vim session.