149

Possible Duplicate:
To switch from vertical split to horizontal split fast in Vim

If I have 2 horizontally split windows, how to rotate them to get 2 vertically split windows?

And how to switch buffers?

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195

4 Answers4

297

If you have them split vertically C-wJ to move one to the bottom

If you have them split horizontally C-wL to move one to the right

To rotate in a 'column' or 'row' of split windows, C-wC-r

The following commands can be used to change the window layout. For example, when there are two vertically split windows, CTRL-W K will change that in horizontally split windows. CTRL-W H does it the other way around.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 13
    I'm finally starting to "feel". Amazing how long I have survived w/o these commands. – Arnis Lapsa May 20 '11 at 11:58
  • 23
    To rotate split windows, I believe you only actually need "C-w r", not "C-w C-r". – Tom Lord Jan 08 '14 at 13:55
  • 4
    @TomLord That works too. Although that's not needing "less". It's just different. And I happen to think "C-w r" is a bit awkward (especially if repeated) – sehe Jan 08 '14 at 14:38
  • 1
    @AmagicalFishy It's just `wincmd` with cursor key mnemonics: http://vimdoc.sourceforge.net/htmldoc/windows.html#:wincmd – sehe Jan 29 '16 at 14:49
  • Doesn't work properly for 4 windows open - rather than flip the windows vertically or horizonally with one another, it pushes the window to a separate level in either direction. For 2, "C-w x" is sufficient – arcseldon Nov 04 '17 at 02:59
  • Reading the `:help CTRL-W_K` helped me remember the keybinds after reading the description of what the keybinds are really doing. – Dylan Landry May 27 '20 at 14:04
103

Ctrl-w H or type :wincmd H to go from horizontal to vertical layout.

Ctrl-w J or type :wincmd J to go from vertical to horizontal layout.

Ctrl-w r or type :wincmd r to swap the two buffers but keep the window layout the same.

Ctrl-w w or type :wincmd w to move the cursor between the two windows/buffers.

You may wish to bind one or more of these sequences to make it faster to type. I put this in my .vimrc so that ,l moves the cursor to the next buffer in the current tab:

let mapleader = ","
nmap <Leader>l <C-w>w
Nick
  • 8,480
  • 3
  • 37
  • 33
  • 1
    @Nick The first two really answer the question and the second two go above and beyond, which is incredibly useful! The "swap" function is particularly useful for me when I translate documents, because I prefer having source languages always on a specific side and I do not always open them in that order :) – Jonathan Komar Sep 05 '16 at 14:06
  • 2
    `Ctrl-w` `r` is the best! – gabe Feb 19 '21 at 03:33
  • Big respect for showing the actual commands to remap this properly. – Martin Braun May 29 '23 at 14:52
17

CTRL-W SHIFT-H will rotate the orientation, CTRL-W H moves to the left window, CTRL-W L moves to the right. See

:help split

and

:help ^w

for more information.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
William Pursell
  • 204,365
  • 48
  • 270
  • 300
10

The current answers all work great if you only have two windows open. If you have more than that, the logic for moving windows around can get hairy.

I have this in my .vimrc to allow me to 'yank' and 'delete' a buffer and then paste it into a window over the current buffer or as a [v]split.

fu! PasteWindow(direction) "{{{
    if exists("g:yanked_buffer")
        if a:direction == 'edit'
            let temp_buffer = bufnr('%')
        endif

        exec a:direction . " +buffer" . g:yanked_buffer

        if a:direction == 'edit'
            let g:yanked_buffer = temp_buffer
        endif
    endif
endf "}}}

"yank/paste buffers
:nmap <silent> <leader>wy  :let g:yanked_buffer=bufnr('%')<cr>
:nmap <silent> <leader>wd  :let g:yanked_buffer=bufnr('%')<cr>:q<cr>
:nmap <silent> <leader>wp :call PasteWindow('edit')<cr>
:nmap <silent> <leader>ws :call PasteWindow('split')<cr>
:nmap <silent> <leader>wv :call PasteWindow('vsplit')<cr>
:nmap <silent> <leader>wt :call PasteWindow('tabnew')<cr>
Greg Sexton
  • 9,189
  • 7
  • 31
  • 37
  • not sure how your function is working - perhaps add a simple visual example – serup Aug 25 '16 at 09:59
  • 1
    This is brilliant. Much better than `C-W J` and friends. I would extend it with `wP` to `top split` and `wV` to `:set nosplitright \| call PasteWindow('vsplit') \| set splitright`. And `wr` makes more sense ('replace') than `we`. – zelk Sep 24 '17 at 14:32