Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Move window left/right?

Tags:

vim

People also ask

How do I move a window in Vim?

To move from the current Vim window to the next one, type CTRL-W j (or CTRL-W <down> or CTRL-W CTRL-J). The CTRL-W is the mnemonic for “window” command, and the j is analogous to Vim's j command, which moves the cursor to the next line.

How do I move left and right in Vim?

To move left, press h . To move right, press l . To move down, press j . To move up, press k .

How do I move between split windows in Vim?

To switch to the right window, press “Ctrl + w”, then “l”. To go to the left window, it's “Ctrl + w”, then “h”. If you did a horizontal split, then going up and down is necessary. For going up, press “Ctrl + w”, then “k”.

How do I move a tab in Vim?

If you're really meticulous and want to position tabs just so in Vim, you can move the tabs to a specific spot in the tab order using :tabm n , where n is the position number that you want to use. If you don't give the :tabm command an argument, then the current tab will be moved to the last spot.


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


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.

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.


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