To switch from vertical split to horizontal split fast in Vim
VimSplitVim Problem Overview
How can you switch your current windows from horizontal split to vertical split and vice versa in Vim?
I did that a moment ago by accident but I cannot find the key again.
Vim Solutions
Solution 1 - Vim
Vim mailing list says (re-formatted for better readability):
> To change two vertically split > windows to horizonally split > Ctrl-w t Ctrl-w K > > Horizontally to vertically: > Ctrl-w t Ctrl-w H > > Explanations: > Ctrl-w t makes the first (topleft) window current
> Ctrl-w K moves the current window to full-width at the very top
> Ctrl-w H moves the current window to full-height at far left
Note that the t is lowercase, and the K and H are uppercase.
Also, with only two windows, it seems like you can drop the Ctrl-w t part because if you're already in one of only two windows, what's the point of making it current?
Solution 2 - Vim
Ctrl-w followed by H, J, K or L (capital) will move the current window to the far left
, bottom
, top
or right
respectively like normal cursor navigation.
The lower case equivalents move focus instead of moving the window.
Solution 3 - Vim
When you have two or more windows open horizontally or vertically and want to switch them all to the other orientation, you can use the following:
(switch to horizontal)
:windo wincmd K
(switch to vertical)
:windo wincmd H
It's effectively going to each window individually and using ^WK or ^WH.
Solution 4 - Vim
The following ex commands will (re-)split any number of windows:
- To split vertically (e.g. make vertical dividers between windows), type
:vertical ball
- To split horizontally, type
:ball
If there are hidden buffers, issuing these commands will also make the hidden buffers visible.
Solution 5 - Vim
In VIM, take a look at the following to see different alternatives for what you might have done:
:help opening-window
For instance:
Ctrl-W s
Ctrl-W o
Ctrl-W v
Ctrl-W o
Ctrl-W s
...
Solution 6 - Vim
># Horizontal to vertical split > Ctrl+W for window command, > > followed by Shift+H or Shift+L
># Vertical to horizontal split > Ctrl+W for window command, > >followed by Shift+K or Shift+J
Both solutions apply when only two windows exist.
After issuing the window command Ctrl+W, one is basically moving the window in the direction indicated by Shift+direction letter.
Opening help in a vertical split by default
Add both of these lines to .vimrc
:
cabbrev help vert help
cabbrev h vert h
cabbrev
stands for command abbreviation.
:vert[ical] {cmd}
always executes the cmd
in a vertically split window.
Solution 7 - Vim
Inspired by Steve answer, I wrote simple function that toggles between vertical and horizontal splits for all windows in current tab. You can bind it to mapping like in the last line below.
function! ToggleWindowHorizontalVerticalSplit()
if !exists('t:splitType')
let t:splitType = 'vertical'
endif
if t:splitType == 'vertical' " is vertical switch to horizontal
windo wincmd K
let t:splitType = 'horizontal'
else " is horizontal switch to vertical
windo wincmd H
let t:splitType = 'vertical'
endif
endfunction
nnoremap <silent> <leader>wt :call ToggleWindowHorizontalVerticalSplit()<cr>
Solution 8 - Vim
Following Mark Rushakoff's tip above, here is my mapping:
" vertical to horizontal ( | -> -- )
noremap <c-w>- <c-w>t<c-w>K
" horizontal to vertical ( -- -> | )
noremap <c-w>\| <c-w>t<c-w>H
noremap <c-w>\ <c-w>t<c-w>H
noremap <c-w>/ <c-w>t<c-w>H
Edit: use Ctrl-w r to swap two windows if they are not in the good order.