693

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.

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

8 Answers8

1044

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?

WEBjuju
  • 5,797
  • 4
  • 27
  • 36
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 35
    So if you have two windows split horizontally, and you are in the lower window, you just use ^WL. – too much php Aug 13 '09 at 02:17
  • 9
    .... The power... My vim-fu doubled today, thank you. There are a ton of interesting ^w commands (b, w, etc) – Alex Hart Dec 07 '12 at 14:10
  • Would be nice is this worked when using the NERDTree plugin :-) – Eno Jun 25 '13 at 20:27
  • Nice one. Thanks. Seems to work find though without making the top window active. – chanHXC Aug 07 '13 at 14:29
  • 4
    @Eno Works fine for me. Just toggle your NERDTree panel closed before 'rotating' the splits, then toggle it back open. :NERDTreeToggle (I have it mapped to a function key for convenience). – Lambart Mar 26 '14 at 19:34
  • 2
    I have added some leader mappings to my vim config: `:nmap th tH` and `:nmap tk tK` – Ian Marcinkowski Jun 08 '16 at 18:13
  • In case anyone is wondering (like me) why `K` and `H`: Those are the **Vim-Movements**, you already know. Maybe I am the only stupid one here who didn't realized, but at least you know now :) – Pixelbog Jan 25 '23 at 11:01
423

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.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
respectTheCode
  • 42,348
  • 18
  • 73
  • 86
55

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.

Steve
  • 6,618
  • 3
  • 44
  • 42
35

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.

Mark
  • 3,357
  • 30
  • 31
  • This should be the accepted answer, as the current accepted answer will only work for two files. This answer is more complete. – DrStrangepork Aug 13 '15 at 17:53
  • `:vert[ical] ball` in the doc so `:vert ball` is also ok and shorter. – Titou May 03 '17 at 09:18
  • `:ball` reduces 3 vertical windows to 2 horizontal windows, while `:vert ball` correctly maps 3 horizontal windows to 3 vertical ones. – Titou May 03 '17 at 09:21
  • @DrStrangepork, it work on all open buffers instead of what is in split'ed windows. For eg: think that you are viewing 3 files in split'ed window out of 10 open buffers. Please see `:ls` for open buffers. Anyways, I like this answer along with other answers. +1 – Robert Ranjan Jul 20 '20 at 18:53
27

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
...

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Anon
  • 11,870
  • 3
  • 23
  • 19
25

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.

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
8

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>
Navidot
  • 327
  • 3
  • 10
  • Works even better for me if I force it to stay in the same split like so `if winnr() == 1 windo wincmd J windo wincmd k else windo wincmd J endif ` and `if winnr() == 1 windo wincmd L windo wincmd h else windo wincmd L endif` Hope that make sense. – mjhoffmann Jan 05 '20 at 09:59
5

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.

Community
  • 1
  • 1
Jabba
  • 19,598
  • 6
  • 52
  • 45