Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Buffers, Windows and Tabs

Windows and Splits

Split the Vim screen horizontally and vertically, move between windows, resize and rearrange them, and learn the Ctrl-w command family with a layout simulator.

A window is a rectangle of the screen that shows a buffer. You can have as many windows as fit, side by side or stacked, each with its own cursor, scroll position and local options. This is how you view a function and its call site together, read documentation next to your code, or compare two files.

Almost every window command starts with Ctrl-w (shown as <C-w>), the "window prefix". The Ex versions start with :split or :vsplit.

Creating splits

Command Key Effect
:split or :sp Ctrl-w s Split horizontally: new window above, same buffer
:vsplit or :vs Ctrl-w v Split vertically: new window to the left
:sp file Split and edit file in the new window
:new / :vnew Ctrl-w n Split with an empty buffer
:sb 3 Split and show buffer 3
vim -o a b / vim -O a b Start with horizontal / vertical splits

The names are confusing at first: split divides the screen with a horizontal line, giving windows stacked on top of each other. vsplit uses a vertical line, so windows sit side by side.

By default the new window appears above or to the left. If you prefer below and right, which feels more natural, put these in your vimrc:

VIM
set splitbelow
set splitright

Moving between windows

Vim uses the same h j k l directions it uses for the cursor.

Key Moves to
Ctrl-w h Window on the left
Ctrl-w j Window below
Ctrl-w k Window above
Ctrl-w l Window on the right
Ctrl-w w Next window (cycles)
Ctrl-w W Previous window
Ctrl-w p Previously used window
Ctrl-w t / Ctrl-w b Top-left / bottom-right window

A count helps: 3 Ctrl-w w jumps to the third window. Many people map Ctrl-h/j/k/l directly to these; you will see how in the mappings lesson.

JS Stand-in: find the neighbour window

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Closing windows

  • Ctrl-w c or :close closes the current window (not the last one).
  • Ctrl-w q or :q quits the window; in the last window it exits Vim.
  • Ctrl-w o or :only keeps only the current window and closes the rest. Great when a screen has got crowded.
  • :qa quits all; :wqa writes all and quits.

Closing a window never deletes its buffer. If the buffer was modified and hidden is off, Vim asks you to save first.

Resizing

Key Effect
Ctrl-w = Make all windows equal size
Ctrl-w + / Ctrl-w - Taller / shorter by one line (use a count: 5 Ctrl-w +)
Ctrl-w > / Ctrl-w < Wider / narrower by one column
Ctrl-w _ Maximize height
Ctrl-w | Maximize width
:resize 15 / :vertical resize 60 Set an exact height / width

A common pattern is Ctrl-w _ followed by Ctrl-w | to zoom the current window temporarily, then Ctrl-w = to restore equal sizes.

JS Stand-in: equal splitting with Ctrl-w =

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Rearranging

  • Ctrl-w x exchanges the current window with the next one.
  • Ctrl-w r / Ctrl-w R rotates windows down/right or up/left.
  • Ctrl-w H, J, K, L (capitals) move the current window to the far left, bottom, top or far right, using the full screen height or width. This also turns a horizontal split into a vertical one.
  • Ctrl-w T moves the window into its own tab page.
flowchart LR
    S["Ctrl-w s / :split"] --> H["Stacked windows"]
    V["Ctrl-w v / :vsplit"] --> Sd["Side-by-side windows"]
    H -->|"Ctrl-w L"| Sd
    Sd -->|"Ctrl-w J"| H

Capital H J K L change the layout; lowercase h j k l only move the cursor.

Running a command in every window

  • :windo set scrollbind ties scrolling of all windows together, useful for comparing files.
  • :windo diffthis turns every window into a diff view (see the git lesson).
  • :windo set nowrap applies a local option everywhere.

Window-local options such as wrap, number and cursorline belong to the window, not to the buffer. If you split a window, the new window inherits them.

Special windows

Help (:help topic) opens in a split. Close it with :q or Ctrl-w c. The preview window (:pedit file, Ctrl-w z closes it), the quickfix window (next lessons), and terminal windows are all ordinary windows with special buffers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command puts two windows side by side?

    :vsplit divides the screen with a vertical line, so the windows sit side by side. :split stacks them.
  2. 2. What does Ctrl-w o do?

    Ctrl-w o is :only. It closes every other window but does not delete their buffers.
  3. 3. How do you move the current window to the far right, full height?

    Capital L moves the window to the far right. Lowercase l only moves the cursor to the window on the right.
  4. 4. Which setting makes new horizontal splits open below the current window?

    splitbelow affects :split; splitright affects :vsplit.

Summary

  • Splits show buffers in several windows; :split stacks, :vsplit sits side by side.
  • Ctrl-w h j k l moves, Ctrl-w = equalizes, Ctrl-w o keeps one window.
  • Capital Ctrl-w H J K L rearranges the layout.
  • splitbelow and splitright make splits open where you read next.
  • :windo runs a command in every window.

Next: Tab Pages and Layouts