Using Split Windows With Vim

Have you ever watched a bearded sysadmin navigate their editor with lightning speed, jumping between multiple panes with the flick of a few keystrokes? That's the magic of Vim's split window feature!

Think of it as having multiple monitors inside a single screen. And you don't even need screen command or tmux tools for this purpose. Vim does it on its own.

Split Windows in Vim Editor

You can split the screen horizontally as well as vertically. And all this is done with a few keystrokes, of course.

Vim split window keyboard shortcuts

Action Keyboard Shortcut Description
Horizontal split :split or :sp Splits window horizontally
Vertical split :vsplit or :vs Splits window vertically
Close current window :q or :close Closes the active window
Close all except current :only or :on Closes all windows except active one
Navigate between windows Ctrl-w + h/j/k/l Move to left/down/up/right window
Navigate between windows Ctrl-w + Ctrl-w Cycle through all windows
Resize horizontally Ctrl-w + < or > Decrease/increase width
Resize vertically Ctrl-w + - or + Decrease/increase height
Equal size windows Ctrl-w + = Make all windows equal size
Maximize height Ctrl-w + _ Maximize current window height
Maximize width Ctrl-w + | Maximize current window width
Move window Ctrl-w + r Rotate windows
Swap with next window Ctrl-w + x Exchange with next window

You can also use the mouse for resizing and some other features if you have mouse enabled in Vim.

Creating Split windows

Let's see in details about how those magical keys work and look like in the editor.

Horizontal splits

Creating a horizontal split in Vim is like adding a floor to your house - you're stacking views on top of each other.

When you're deep in code and need to reference something above or below your current focus, horizontal splits come to the rescue.

:split filename  (or :sp filename)
0:00
/0:11

Horizontal split in action

Filename is optional here. If you don't specify a filename, Vim will split the window and show the current file in both panes.

💡
Use :set splitbelow to open the new windows below the current one.

Vertical splits

Vertical splits open windows side-by-side. It's good for viewing documentation, or keeping an eye on multiple parts of your project. You can also use it for quickly comparing two files if you do not want to use the dedicated vimdiff.

:vsplit filename  (or :vs filename)
0:00
/0:12

Vertical split in action

💡
By default, the new windows are opened on the left of the current window. Use :set splitright to open the new windows on the right.

Moving between split windows

Once you've created splits, hopping between them is where the real productivity starts. Think of Ctrl-w as your magic wand - when followed by a direction key, it teleports your cursor to that window.

  • Ctrl-w followed by w will switch to the below/right window.
  • Ctrl-w followed by W (uppercase W or Shift-w key) will switch to the above/left window.

I prefer the direction keys, though. It is easier to navigate this way in my opinion.

Ctrl-w h  # Move to the window on the left
Ctrl-w j  # Move to the window below
Ctrl-w k  # Move to the window above
Ctrl-w l  # Move to the window on the right
0:00
/0:15

Move between splits

You can also use the arrow keys instead of the typical hjkl Vim movement keys.

💡
If remembering directions feels like a mental gymnastics routine, just cycle through all the windows by pressing Ctrl-w Ctrl-w. Pressing them multiple times in pair and you'll move from one split window to the next.

I'll be honest - when I first started using Vim, I kept forgetting these window navigation commands. So, I thought of Ctrl-w as "window" followed by the direction I wanted to go. After a few days of practice, my fingers remembered even when my brain forgot!

Resizing split windows

Not all windows are created equal. Some need more space than others, based on your need. Vim lets you adjust your viewing space with a few keystrokes.

Ctrl-w +  # Increase height by one line
Ctrl-w -  # Decrease height by one line
Ctrl-w >  # Increase width by one column
Ctrl-w <  # Decrease width by one column
0:00
/0:21

Resize split windows

For faster resizing, prefix these commands with a number:

10 Ctrl-w +  # Increase height by 10 lines

When things get too chaotic, there's always the great equalizer:

Ctrl-w =  # Make all windows equal size

Just so that you know, you can also create splits with specific dimensions by adding a number before the command.

For example, :10split creates a horizontal split with 10 lines of height, while :30vsplit creates a vertical split that's 30 characters wide.

💡
Need maximum space ASAP? Try these power moves:

Ctrl-w _ maximizes the current window's height
Ctrl-w | maximizes the current window's width
Ctrl-w = equalizes all windows when you're ready to share again

I call this the "focus mode toggle" - perfect for when you need to temporarily zoom in on one particular section of a file!

Moving and rearranging Windows

Sometimes you create the perfect splits but realize they're in the wrong order. Rather than closing and recreating them, you can rearrange your windows like furniture:

Ctrl-w r  # Rotate windows downward/rightward
Ctrl-w R  # Rotate windows upward/leftward
Ctrl-w x  # Exchange current window with the next one
0:00
/0:28

Rearrange split windows

You can also completely move a window to a new position:

Ctrl-w H  # Move current window to far left
Ctrl-w J  # Move current window to very bottom
Ctrl-w K  # Move current window to very top
Ctrl-w L  # Move current window to far right

It's like playing Tetris with your editor layout. While I am a fan of the classic Tetris game, I am not a fan of moving and rearranging the windows unless it is really needed.

💡 Few random but useful tips

Let me share a few more tips that will help your workflow when you are dealing with split windows in Vim.

Add terminal in the mix

If you are in a situation where you want to look at your code and run it at the same time, like an IDE, you can add a terminal in your split. No more alt-tabbing between terminal and editor!

  • :sp | terminal opens a horizontal split with a terminal
  • :vs | terminal opens a vertical split with a terminal
Terminal is split window in Vim

Start split with Vim

Want to start Vim with splits already configured? You can do that from your command line:

# Open two files in horizontal splits
vim -o file1 file2

# Open two files in vertical splits
vim -O file1 file2

# Open three files in horizontal splits
vim -o3 file1 file2 file3

File explorer in split windows

One of my favorite tricks is opening Vim's built-in file explorer (netrw) in a split:

:Sexplore  # Open file explorer in horizontal split
:Vexplore  # Open file explorer in vertical split
Vim file explorer in split window

It's like having a mini file manager right next to your code - perfect for quickly navigating project files without losing your place in the current file.

Close all the split windows and exit Vim

When you are done with your tasks on a project, instead of closing individual split windows, you can close all of them together and exit Vim :qa

Save your work before, of course.

Wrapping up

Splits aren't just a cool feature - they're a strategic tool. You can use it to edit files with reference doc open on the side or to watch log output in the terminal while debugging. It's up to you how you use this amazing feature.

It might seem like keyboard gymnastics at first, but quickly becomes second nature. Like learning to touch type or ride a bike, the initial awkwardness gives way to fluid motions that you'll hardly think about.

Start small - maybe just two vertical splits - and gradually incorporate more commands as you get comfortable. Before long, you'll be that expert terminal dweller others watch in amazement as you effortlessly dance between multiple files without ever touching your mouse. Happy splitting! 🚀