260

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}.

0

5 Answers 5

502
+50

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

6
  • 3
    <c-w> r only rotates vertically. Dec 31, 2010 at 18:32
  • 11
    @David Wolever, you need to use the upper case letters in this case, something like: Ctrl-W + Shift-L, for example. I think this can be what you are looking for. Feb 9, 2011 at 20:28
  • @David - just tested this and <c-w> r worked horizontally for me.
    – ostler.c
    Jan 9, 2013 at 19:18
  • 3
    @ostler.c create a vertical split, then create a horizontal split within one of the columns. Now use <c-w> r and it only cycles the two windows within the one vertical split.
    – Chev
    Dec 19, 2013 at 18:08
  • 1
    @nn0p no, they move windows around. <c-w>h will move cursor to the next window to the left, while <c-w>H will move current window (together with cursor) to the far left.
    – MarSoft
    Jul 27, 2017 at 23:27
72

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.
1
  • very useful when you're in the middle of the panes and want to swap with the one on the right. Thanks for this one! Mar 5, 2018 at 7:45
11

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.

3
  • 1
    I want to move the windows, not the cursor. Dec 31, 2010 at 18:33
  • 1
    Then this is as far as you can go with the vim window commands, if this was usefull to you please mark it as the solution :-)
    – Bitterzoet
    Jan 5, 2011 at 11:40
  • 3
    I would downvote @Bitterzoet 's comment if I could. See above answers Jul 5, 2018 at 0:20
2

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
-9
  • ctrl + h move cursor to next window(right)

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

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

1
  • 6
    Not correct. Ctrl+h/l are not standard bindings for window navigation (although they can of course be configured, and are offered by some plugins). And this doesn't answer the original question anyway.
    – MarSoft
    Aug 1, 2017 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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