Vim: Move window left/right?

Vim

Vim Problem Overview


In Vim, is it possible to “move” a window to the left or right? Eg, similar to <c-w> r or <c-w> x, but left/right instead of up/down?

For example, if I've got this layout:

+---+---+---+
|   |   +---+
| A +---+---+
|   |   |   |
+---+---+---+

I'd like to turn it into this:

+---+---+---+
|   |   +---+
+---+ A +---+
|   |   |   |
+---+---+---+

Which is difficult/annoying to do with <c-w> {H,J,K,L}.

Vim Solutions


Solution 1 - Vim

Ctrl w gives you the "windows command mode", allowing the following modifiers:

  • Ctrl w + R - To rotate windows up/left.

  • Ctrl w + r - To rotate windows down/right.

You can also use the "windows command mode" with navigation keys to change a window's position:

  • Ctrl w + L - Move the current window to the "far right"

  • Ctrl w + H - Move the current window to the "far left"

  • Ctrl w + J - Move the current window to the "very bottom"

  • Ctrl w + K - Move the current window to the "very top"

Check out :help window-moving for more information

Solution 2 - Vim

This one is the most useful for me (and is probably the right answer to the question):

  • Ctrl W + x OR Ctrl W + Ctrl x - Rotates the current focused window with the closest window to the right.

Solution 3 - Vim

Do you want to move the window itself or just your cursor position?

Next to rotating or cycling like you already mentioned, it's only possible to move the window itself to the far top, bottom, left or right, with respectively:

^W K
^W J
^W H
^W L

I don't think there is a default builtin way to moving a window one place to the right.

Solution 4 - Vim

It really seems like vim can't do this with the standards key maps. The documentation says that the ^W K, J, H and L commands work by creating the split and opening the buffer in the now position, so I wrote a function to the same: Hide the buffer, move to the left, split, and then open the original buffer:

" Rotate a window horizontally to the left
function! RotateLeft()
    let l:curbuf = bufnr('%')
    hide
    wincmd h
    split
    exe 'buf' l:curbuf
endfunc

" Rotate a window horizontally to the right
function! RotateRight()
    let l:curbuf = bufnr('%')
    hide
    wincmd l
    split
    exe 'buf' l:curbuf
endfunc

Solution 5 - Vim

  • ctrl + h move cursor to next window(right)

  • ctrl + l move cursot to previous window(left)

about more information, please refer: :help window-moving

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
QuestionDavid WoleverView Question on Stackoverflow
Solution 1 - VimRicardo ValerianoView Answer on Stackoverflow
Solution 2 - VimfabiomcostaView Answer on Stackoverflow
Solution 3 - VimBitterzoetView Answer on Stackoverflow
Solution 4 - VimW KlinkView Answer on Stackoverflow
Solution 5 - VimxautjzdView Answer on Stackoverflow