How to flip windows in vim?

Vim

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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

Solution 3 - Vim

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.

Solution 4 - Vim

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArnis LapsaView Question on Stackoverflow
Solution 1 - VimseheView Answer on Stackoverflow
Solution 2 - VimNickView Answer on Stackoverflow
Solution 3 - VimWilliam PursellView Answer on Stackoverflow
Solution 4 - VimGreg SextonView Answer on Stackoverflow