150

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?

2
  • Switch buffers? Or windows?
    – johnsyweb
    Commented May 20, 2011 at 11:55
  • 1
    @Johnsyweb switch buffers, rotate windows. But I got it... Commented May 20, 2011 at 11:55

4 Answers 4

301

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.

6
  • 13
    I'm finally starting to "feel". Amazing how long I have survived w/o these commands. Commented May 20, 2011 at 11:58
  • 23
    To rotate split windows, I believe you only actually need "C-w r", not "C-w C-r".
    – Tom Lord
    Commented Jan 8, 2014 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
    Commented Jan 8, 2014 at 14:38
  • 1
    @AmagicalFishy It's just wincmd with cursor key mnemonics: vimdoc.sourceforge.net/htmldoc/windows.html#:wincmd
    – sehe
    Commented Jan 29, 2016 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
    Commented Nov 4, 2017 at 2:59
104

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
3
  • 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 :) Commented Sep 5, 2016 at 14:06
  • 2
    Ctrl-w r is the best!
    – gabe
    Commented Feb 19, 2021 at 3:33
  • Big respect for showing the actual commands to remap this properly. Commented May 29, 2023 at 14:52
19

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.

3
  • That one is for switching buffers. How to change direction of windows? Commented May 20, 2011 at 11:43
  • 1
    @Arnis ^wH will change the orientation of the window layout. ^wh moves to the window on the left. The operations are case sensitive. Commented May 20, 2011 at 12:12
  • AWESOME! I Love it <C-w>H and <C-w>L
    – Developer
    Commented Jun 17, 2015 at 19:57
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>
2
  • not sure how your function is working - perhaps add a simple visual example
    – serup
    Commented Aug 25, 2016 at 9:59
  • 1
    This is brilliant. Much better than C-W J and friends. I would extend it with <leader>wP to top split and <leader>wV to :set nosplitright \| call PasteWindow('vsplit') \| set splitright<cr>. And <leader>wr makes more sense ('replace') than <leader>we.
    – zelk
    Commented Sep 24, 2017 at 14:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.