UPDATE: With respect to terminology, check out Drew Neil’s comment below.
In Vim, I’ve been using splits for years. Splits are great:
- view 2 files at the same time
- view 2 parts of the same file at the same time
- dump bits of text into a new split
- dump command outputs into a new split
- and so on…
However, I’ve been using the subset of splits that I understood while shying away from advanced use cases. Somewhere down my TODO list, there was an item called “understand Vim splits”. This blog post is an attempt to document what I discovered.
3 Questions
When it comes to splitting, there are, thankfully, only 3 questions:
- are you splitting the buffer or the window?
- are you splitting horizontal or vertical?
- do you want to send the split left, right, up or down?
When you type:
:split
You are using the defaults: buffer, horizontal, up.
There are 8 combinations:
window horizontal up --> :topleft split window horizontal down --> :botright split window vertical left --> :topleft vsplit window vertical right --> :botright vsplit buffer horizontal up --> :leftabove split buffer horizontal down --> :rightbelow split buffer vertical left --> :leftabove vsplit buffer vertical right --> :rightbelow vsplit
What were they thinking?! Good time to give up? :-D
Illustrated
Look at the following picture. Starting from a initial state, follow what happens when you invoke these commands. (click to enlarge)
- for this example, it doesn’t matter whether you’re using split/vsplit or new/vnew
- the blue buffer is where your cursor is
- the buffers are numbered to help locate them before and after
Even though I spent a few hours thinking about splits and studying the commands to eventually come up with that summary graph, I can’t say it’s the most intuitive set of commands around. If I stop everything I’m doing, I can mentally come up with the right command but it’s very taxing.
Here’s a list of mappings I just added to my .vimrc
" window
nmap <leader>sw<left> :topleft vnew<CR>
nmap <leader>sw<right> :botright vnew<CR>
nmap <leader>sw<up> :topleft new<CR>
nmap <leader>sw<down> :botright new<CR>
" buffer
nmap <leader>s<left> :leftabove vnew<CR>
nmap <leader>s<right> :rightbelow vnew<CR>
nmap <leader>s<up> :leftabove new<CR>
nmap <leader>s<down> :rightbelow new<CR>
Feel free to replace the arrow keys (up, down, left, right) with k, j, h, l if you’re more comfortable with those bindings.