Windows and Splits
[!NOTE] This chapter covers how to transform your single Vim terminal into a multi-pane development environment using Windows and Splits. We’ll explore the mental model of a “Window” as a viewport into a “Buffer”, rather than a file itself.
1. Introduction to Vim Windows
In most modern editors, clicking a file opens a new tab. In Vim, a Window is simply a viewport that displays a Buffer (an in-memory version of a file).
You can have multiple windows showing the same buffer at different lines, or different windows showing different buffers. Mastering windows allows you to view tests alongside implementation, or reference documentation while writing code—all without leaving your terminal.
2. Creating Splits
You divide your current window into multiple panes using horizontal or vertical “splits.”
Horizontal Splits
A horizontal split divides the screen into a top and bottom pane.
- Command:
:splitor:sp - Shortcut:
Ctrl-w s
By default, this opens a new window displaying the same file you are currently editing. To split and open a different file simultaneously:
:sp path/to/file.txt
Vertical Splits
A vertical split divides the screen into a left and right pane. This is especially useful on modern widescreen monitors.
- Command:
:vsplitor:vsp - Shortcut:
Ctrl-w v
Similar to horizontal splits, you can specify a file:
:vsp path/to/other_file.go
3. Interactive Split Visualizer
Vim Split Simulator
Click the buttons to simulate splitting a Vim window. Observe how the space divides.
4. Window Navigation
Once you have multiple windows open, you need to move your cursor between them. The prefix for almost all window commands is Ctrl-w (Control + W).
After pressing Ctrl-w, you use the standard Vim directional keys to indicate where you want to go.
| Shortcut | Action |
|---|---|
Ctrl-w h |
Move cursor to the window on the Left |
Ctrl-w j |
Move cursor to the window Below |
Ctrl-w k |
Move cursor to the window Above |
Ctrl-w l |
Move cursor to the window on the Right |
Ctrl-w w |
Cycle through all open windows sequentially |
Ctrl-w p |
Jump back to the Previously accessed window |
[!TIP] Map
Ctrl-wcombinations? Many Vim users find typingCtrl-w htedious. A very common.vimrcpattern is mappingCtrl-htoCtrl-w h, allowing you to jump panes instantly with a single modifier combo.
5. Resizing Windows
When splitting windows, Vim divides the available screen space equally. Often, you want your primary code file to take up 80% of the screen, while a terminal or test file takes up 20%.
You can resize windows using Ctrl-w followed by a math operator.
| Shortcut | Action |
|---|---|
Ctrl-w = |
Make all windows exactly Equal size |
Ctrl-w + |
Increase current window Height |
Ctrl-w - |
Decrease current window Height |
Ctrl-w > |
Increase current window Width |
Ctrl-w < |
Decrease current window Width |
Applying Multipliers: Like most Vim commands, these accept a count. Typing 10 Ctrl-w + increases the height by 10 lines at once.
Alternatively, you can use Ex commands for precise sizing:
:resize 20(Sets height to exactly 20 lines):vertical resize 80(Sets width to exactly 80 columns)
6. Closing & Moving Windows
To close a split window, you can use the standard quit commands:
:q(Quits the current window. If it’s the last window, Vim exits):hide(Hides the window, but keeps the buffer active in memory)Ctrl-w c(Closes the current window)Ctrl-w o(Only. Closes all other windows except the active one. Essential when you are done referencing other files and want to focus.)
Moving Windows Around
Sometimes you open a horizontal split but realize you’d prefer a vertical one. You can physically move the windows themselves:
Ctrl-w H(Capital H). Move the current window to the far Left, creating a vertical split.Ctrl-w J(Capital J). Move the current window to the Bottom, creating a horizontal split.Ctrl-w K(Capital K). Move the current window to the Top.Ctrl-w L(Capital L). Move the current window to the far Right.