Split Vim Workspace Vertically or Horizontally
Vim allows you to have multiple horizontal or vertical splits in your active workspace.
— Team LHB
As you get comfortable with the basics of Vim and start exploring it even more deeply, you discover new things.
New things like splitting the screen while using Vim in a terminal. No need of tmux or screen. Just do it under Vim, natively.
Yes, Vim allows you to have multiple horizontal or vertical splits in your active workspace.
Let me show you how to split Vim and all the necessary keyboard shortcuts to navigate between the split windows.
Create split window
Suppose you opened a file in Vim. Now you want to split the workspace into multiple windows for better productivity. Let's go over how to create a split window in Vim.
There are two ways you can split a Vim workspace - horizontally and/or vertically.
You can know which window is active based on which window has your cursor.
Split window vertically
Assume that you have opened a file in Vim, and you want to split the screen vertically.
To make a vertical split, enter Normal mode, and run the following command:
If you specify a file path, it will open the file in the newly split window, otherwise, the newly split window will open the same file.
A shorter command to
vs (you can specify a file path with
vs as well).
As an alternative way to create a vertical split, you can press the Ctrl + w key combination, and lastly, press the letter "v" (v for vertical split).
Split window horizontally
Vim allows you to split the window horizontally as well.
To make a horizontal split, enter Normal mode, and run the following command:
If you specify a file path, it will open the file in the newly split window, otherwise the newly split window will open the same file.
A shorter command to perform a horizontal split is to use the
sp command. It also accepts a file path.
If you are wondering if any key combination exists for creating a horizontal split window, yes. It is "Ctrl + w" and then press the letter "s".
Close split windows
There are several ways you can close/quit an active split window.
:q[uit]- close the current window and the buffer as well
:bd[elete]- unload the current buffer and then close the current window
:on[ly]- close all other windows but leave all buffers open
Navigating split windows
Consider the following scenario, I have the Vim workspace divided into four quadrants.
Below are the four operations you can perform and the key combinations.
- Move to the split window on the left: press Ctrl + w and press h
- Move to the split window on the down: press Ctrl + w and press j
- Move to the split window on the up: press Ctrl + w and press k
- Move to the split window on the right: press Ctrl + w and press l
Resizing split windows
By default, Vim creates splits with a similar width/height. It is good for my OCD, but not really productive when I have a file that I edit most of the time and another file that I rarely edit.
So, let's go over how you can resize the split windows in Vim.
To resize a window, use either of the following methods:
- Press Ctrl + w key combination [optionally specify a number] and then press the "+" (plus) symbol to increase the height of the current window
- Press Ctrl + w key combination [optionally specify a number] and then press the "-" (minus) symbol to reduce the height of current window
- Press Ctrl + w key combination [optionally specify a number] and then press the "<" (greater than) symbol to reduce the width of the current window
- Press Ctrl + w key combination [optionally specify a number] and then press the ">" (less than) symbol to increase the width of the current window
Expand the window as much as possible
Below are the key combinations that you can press to expand a vertically split window vertically or a horizontally split window horizontally.
- Expand vertically - press Ctrl + w and then press the pipe "|" character (the character which gets typed when you press the backslash key while holding down Shift)
- Expand horizontally - press Ctrl + w and then press "_"
Reset the size of all resized windows
To reset the size of all split windows, press Ctrl + w and then "=". This will resize all the windows and make them equal.
More tips to improve your workflow
Let's go over some tips that I think you might find useful with a workflow that includes creating multiple split windows in a Vim workspace.
Open a new file/buffer
This will open the same file in newly split window. To open another file in new window, use either
:open command followed by the file name to make the window split actually usable.
Copy and paste
This is not actually a tip, but when you copy or cut something from a split window and move to another window and try to paste, it will work. No additional configuration is necessary.
If you look closely at navigating between active windows, it is similar to moving your cursor's direction. Except, there is a need to press "Ctrl + w" every time and then press either h,j,k,l keys.
Below is what I personally use. Instead of pressing "Ctrl + w" and then pressing h,j,k,l keys, you can simply press "Ctrl + [h,j,k,l]".
If you want to use these key combinations, add the following keys to your 'vimrc':
nnoremap <C-J> <C-W><C-J> nnoremap <C-K> <C-W><C-K> nnoremap <C-L> <C-W><C-L> nnoremap <C-H> <C-W><C-H>
By default, when you create a vertically split window, it will open to the left. To change this default behavior, add the following line to your 'vimrc'.
Similarly, if you create a new horizontally split window, it will open on the topmost portion of the Vim workspace. To make a new horizontally split window open on the bottom of current window, add the following line to your 'vimrc'.
This article covers how you can create one or more horizontal or vertical splits in an active Vim workspace. We also go over how you can resize split windows, close an active split window and how to navigate between open windows.
Interested in more advanced Vim topics? Go for this highly recommended Vim course.