Vim splits aren’t just about seeing more files; they’re about manipulating your visual context within a single, shared buffer.
Let’s see this in action. Imagine you’re editing a Go file and want to reference its companion test file.
:sp my_file.go
:vs my_file_test.go
Now you have my_file.go in the top half and my_file_test.go in the bottom half. You can move between them with Ctrl-w j (down) and Ctrl-w k (up). Notice the cursor in my_file_test.go is in the exact same line number as in my_file.go. This is no accident.
This shared visual context is the core of Vim’s split power. When you split a window, you’re not creating a new instance of the file. You’re creating a new view into the same underlying buffer. This means changes in one split are immediately reflected in all other splits viewing that same buffer. It’s like having multiple rulers on the same piece of paper.
Here’s the mental model:
- Buffer: The raw content of your file.
- Window: A viewport onto a buffer.
- Split: Creating a new window that shares a buffer with an existing window, or a new buffer entirely.
You can split horizontally (:sp or :split) or vertically (:vs or :vsplit).
:spcreates a horizontal split. The new window appears below the current one.:vscreates a vertical split. The new window appears to the right of the current one.
You can also specify a file: :sp other_file.txt or :vs another_file.txt. If no file is given, Vim splits the current window’s buffer.
Resizing is intuitive. Use Ctrl-w followed by an arrow key and a number. For example, to make the current window 10 lines taller, type Ctrl-w 10 Ctrl-w +. To make it 5 lines narrower (for vertical splits), type Ctrl-w 5 Ctrl-w -.
To close a window, use :q or :close. If you close the last window viewing a buffer, and that buffer has unsaved changes, Vim will complain. If you close the last window on screen, Vim will exit (unless you have other windows open in other tabs).
You can also jump between windows using Ctrl-w followed by h, j, k, or l (which map to left, down, up, right, respectively). For example, Ctrl-w h moves to the window on the left.
The true magic happens when you start thinking about multiple buffers in multiple windows. You can open a file in a new split without leaving your current file:
:vs
:buffer another_file.txt
Now you have two files side-by-side. You can cycle through your open buffers with :bnext and :bprev.
The key to mastering splits is understanding the buffer-window relationship. When you split, you’re often creating a new window pointing to the same buffer. This is why :sp without arguments is so powerful for comparing different sections of a large file. You’re literally looking at the same text, just in different places on your screen. If you want to edit two different files side-by-side, you’d typically use :sp filename or :vs filename.
When you create a split with :sp or :vs, Vim defaults to dividing the space equally. However, you can explicitly control the size. For a horizontal split, :20sp will create a new window that is 20 lines tall. For a vertical split, :80vs will create a new window that is 80 columns wide. These commands create the new window with the specified dimension, and the remaining space is given to the original window.
The relationship between windows and buffers is what allows for powerful techniques like synchronized scrolling. If you have two windows viewing the same buffer, you can enable synchronized scrolling with :set scrollbind. Now, when you scroll in one window, the other will follow, keeping your context aligned. This is invaluable for large configuration files or codebases where you need to see related sections simultaneously.
The next logical step is exploring Vim’s tab pages, which offer another layer of organization for your windows.