Video

Want to see the full-length video right now for free?

Notes

Windows & Tabs

Often you'll want to be able to view multiple files at the same time, for instance to view a test file, and the associated implementation file, e.g. spec/models/user_spec.rb and app/models/user.rb. Thankfully, Vim has great support for arranging your display exactly as you need using its windows and tabs.

Nomenclature

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

"Windows" are often referred to as "Splits" when the tab page has been split.

Windows

Opening Windows

Command Operation
:new [filename] Open a new window above the current window
:vnew [filename] Open a new window beside the current window
:split <filename> Edit the specified file in new window above the current window
:vsplit <filename> Edit the specified file in new window beside the current window

Moving Between Windows

The majority of Vim's window-related commands can be accessed by hotkeys that are all prefixed with <C-w> (Ctrl and w at the same time, followed by the additional key).

Mapping Operation
<C-w>h,j,k,l Navigate to the window in the given direction (<C-w>j navigates down)
<C-w>H,J,K,L Move the current window in the given direction (<C-w>J moves it down)
[count]<C-w>- Decrease the height of the current window by count
[count]<C-w>+ Increase the height of the current window by count
[count]<C-w>< Decrease the width of the current window by count
[count]<C-w>> Increase the width of the current window by count
<C-w>= Equalize the width and height of all windows

Positioning the Buffer In the Window

Often you'll want to reposition the current file within its window. Vim provides the following mappings for this task:

Mapping Operation
zz Center the current line within the window
zt Bring the current line to the top of the window
zb Bring the current line to the bottom of the window

Tabs

Tabs, or "tabpages", are a collection of windows in Vim. At any given time, your Vim session will be viewing a single tab. You can open new tabs as needed to allow quick access to multiple different files.

Command / Mapping Operation
:tabnew Open a new tab
:tabedit <filename> Edit the file with the provided name in a new tab
gt Go to next tab (wraps around to beginning)
gT Go to previous tab (wraps around to end)
<C-w>T Break the current window out to a new tab

The above commands and mappings provide a great deal of flexibility, and none is best in all situations. We recommend you try out various configurations, orientations, and organization styles to find out what works best for you and your project.