Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip windows in vim? [duplicate]

Tags:

vim

People also ask

How do I switch between screens in Vim?

Control + W followed by W to toggle between open windows and, Control + W followed by H / J / K / L to move to the left/bottom/top/right window accordingly, Control + W followed by Left / Down / Up / Right arrow to move to the left/bottom/top/right window accordingly.

How do I open two windows side by side in Vim?

Splitting Vim Screen Horizontally To split the vim screen horizontally, or open a new workspace at the bottom of the active selection, press Ctrl + w , followed by the letter 's' .


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.


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

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.


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>