# Split Windows In Vim

Split windows are one of my favorite features. Many modern editors allow you to see multiple documents on the same screen in a “split window” but very few of them allow you to see different parts of the same document in split windows.

I find this to be incredibly useful when working on large class files. Imagine you’ve got an unfamiliar method defined near the bottom of a file, but it is called from the top of the file. In that case I’ll split the window and put the method in my bottom window for reference while I edit where it’s being called from in the other split, or vice-versa.

In this post I’ll be covering the basics of working with split windows. If you’re interested in learning how to style them visually check out the post on Split Window Aesthetics.

You can split any window horizontally or vertically. This refers to the direction of the dividing line between windows. So, a horizontal split would divide your window into a top and bottom half. A vertical split would divide it left and right. By default the new window will have the same document in it, scrolled to the same place.

Closing one of these split windows is easy. You’ve probably already done it. :q or :quit will quit that window just like any other.

## Horizontal Splits

:split or :sp splits your window horizontally. The “new” split is the one on the top. If you’d like the new split to be below then add this to your ~/.vimrc

set splitbelow " new horizontal splits are on the bottom


For quicker invocation you can type <CTRL>ws

If you’d like to open a new horizontal split with an empty buffer you can type <CTRL>wn

## Vertical Splits

:vsplit or :vs splits your window vertically. The “new” split is the one on the left. If you’d like the new split to be the one on the right then add this to your ~/.vimrc

set splitright " new vertical splits are on the right


For quicker invocation you can type <CTRL>wv

If you’d like to open a new verticaal split with an empty buffer you’ll want to use :vnew. There doesn’t appear to be a key combo to invoke this.

## Opening another file in your split

You can open a new split that starts with a different file in it if you pass in the path to that file as an argument to :split (or :vsplit) For example:

:split path/to/awesome.rb

If you don’t know the full path you can open the split without an argument and then use any of the other methods discussed in opening files) to replace the contents with something from another file.

## Working with splits

“w” is for window. there are a bunch of commands for working with split windows that begin with <CTRL>w

### Switching between splits

<CTRL>ww will jump your cursor to the next split.

### Resizing splits

The easiest way to resize a split is to move your mouse onto the divider and drag it to where you want it. sadly, that only works in GUI clients. So, how do you do it without a mouse?

<CTRL>w+ (that’s literally hitting control w and then plus) and <CTRL>w- will increase or decrease the height of a horizontal split.

<CTRL>w< and <CTRL>w> will move a vertical split left or right respectively.

If you know how many lines tall you want a horizontal split to be (easy with relative line numbers) you can tell Vim to resize to exactly that many lines with :resize 12 (or any other number). You can’t screw this up by entering a number that’s too high or too low. No window will ever have less than 1 line.

Speaking of less than one, and relative line numbers. Care to guess what :resize -1 does? It makes your window one line shorter. :resize +1 makes it one taller. I like this better than the <CTRL>w commands because “I just need a couple more lines” doesn’t require me to count how many lines there already are.

To do the same thing for vertical splits you use :vertical resize +12 to add twelve more columns, or :vertical resize 12 to make it exactly twelve columns wide. That’s just not fair to vertical split users. I’m sorry. I’ll leave it as an exercise to the reader to create a vresize function.

Note that by default horizontal splits are half the height of the current window, and vertical splits are half the width.

Check out the help docs for equalalways for more about setting default split sizes.

## Moving Between Splits

<CTRL>ww (control w twice) will move your cursor to the next window. Keep doing it and it will cycle between all your splits. If you only have two it’ll just go back and forth.

<CTRL>w plus a movement command (h,j,k,l or the arrow keys) will switch your cursor to that window.

## Make it stop!

Want get rid of all the splits but the current one? Move your cursor to the split you want to save and run :only. For quicker invocation you’ll want the <CTRL>wo key combo.

## More

There are a number of subtle variations you can do with regards to creating splits. You don’t really need any of it, but you may want it. I would note that the docs are hidden under :help :split not :help split because split() is a function for splitting strings and looking up the help without the colon finds that instead, unlike most everything else in help.