Say the following is the current structure of my window:

|     |
|  A  |
|     |
|     |
|  B  |

The order of A and B is not quite natural. It would be better if B was on top.

However, if I go <C-w>K, B will be the very top of all windows, which is not what I want. How can I put B above A, but not above any other window that is already above A?

And if two windows are vertically split, how can I change them to horizontally split?

  • 7
    This post explains how to rotate windows. This is about switching splits from vertical to horizontal.
    – lcd047
    Jun 27 '15 at 5:51
  • @lcd047 Those are questions outside of this site. Is it worth writing a small answer here and include the links (which are valuable)?
    – Bernhard
    Jun 27 '15 at 6:02
  • 1
    @Bernhard I suppose this is a good question for meta.
    – lcd047
    Jun 27 '15 at 6:05
  • 1
    @Bernhard Yes. Make sure to write a complete answer, not just links. Jun 27 '15 at 10:45
  • 1
    Then please explain what you're trying to do and why the two answers I pointed you to don't address the problem.
    – lcd047
    Jun 27 '15 at 12:51

To swap the two parts of a split window simply do: <C-w> <C-r>

  • 1
    I may be wrong but your solution rotate the windows so with more than 2 split the result will not be what OP was looking for.
    – statox
    Dec 30 '16 at 10:13
  • You are absolutely right. The OP was looking for for s.th. more complicated than "swap the two parts of a split window".
    – modesto
    Jan 1 '17 at 11:07
  • 1
    Actually OP is looking for a command to swap two split windows but your solution rotate all of the current split windows. As stated in :h CTRL-W_CTRL-R: The second window becomes the first one, the third one becomes the second one, etc. Thus if you have 3 splits, the cursor on the bottom one and hit <c-w><c-r> the 3rd window will be on top of the 2 other ones which is not what OP was asking for: How can I put B above A, but not above any other window that is already above A?
    – statox
    Jan 2 '17 at 11:12
  • 1
    You don't really need <C-w> <C-r> , <C-w> r should be enough.
    – 0xc0de
    Mar 9 at 11:27
  • How do we swap column wise? All the solutions given here swap the rows but how to swap the columns? For example in a 4 window split layout. Jun 23 at 11:12
CTRL-W x           exchange current window with window N
                   (default: next window)

(index.txt section 2.2, "Window commands")

If A is the current window, then the next will be B, so you only need <C-w> x

If B is the current window (and there are more windows below) then you'll first have to go to A, then exchange: <C-w> k <C-w> x


Another alternative to swapping windows or swapping the buffers, is to mark positions (using uppercase marks) in your files and open up those marks in the windows of your choosing.

For example if I have two files, file_A and file_B:

  • I would go the window with file_A and mark it: mA
  • go to the window with file_B and mark it: mB
  • Then fetch the mark of file_A into that window: 'A
  • then go back to the first window and fetch the mark of file_B in there: 'B.

The advantage of this method is if for some reason you split your pane more than once and there is no clear logic to swapping them around, like if you want to swap the panes at 12 o'clock and 7 o'clock, you can't just rotate the windows around but you can easily swap the buffers.

  • This one is useful as it works with a wider array of complex window arrangements
    – Henry E
    Apr 1 '20 at 9:47
  • 1
    Can't believe I didn't think of this. Wonderfully simple, efficient, and elegant solution. May 7 at 13:49

In order to swap window positions, the effect is equivalent to swapping buffers that those two windows show.
e.g. If window A has buffer 2 and window B has buffer 8, you'll want to go to Window A, select buffer 8, then to Window B, select buffer 2.

This idea inspired me to script it - and it's already in my own toolbox as well

Function Code:

function! WinBufSwap()
  let thiswin = winnr()
  let thisbuf = bufnr("%")
  let lastwin = winnr("#")
  let lastbuf = winbufnr(lastwin)

  exec  lastwin . " wincmd w" ."|".
      \ "buffer ". thisbuf ."|".
      \ thiswin ." wincmd w" ."|".
      \ "buffer ". lastbuf

command! Wswap :call WinBufSwap()
map <Leader>bs <C-c>:call WinBufSwap()<CR>
  1. Select window A
  2. Select window B
  3. :call WinBufSwap()
    This swaps the buffers in the two pre-selected windows, leaving the cursor in the original window.

Selecting the two desired windows to be swapped ahead of time is the key detail, as that is needed to specify for Vim which ones to work with.

  • 1
    Just a precision, when you say "select the window A, then B", do you mean "Have the focus on A, then on B, to mark the order of the last seen buffers and windows, to swap properly? Or does "select" mean something else than "have the focus on"?
    – padawin
    Apr 16 '19 at 20:48
  • Yes exactly: I’m using “select” as “focus on”.
    – Cometsong
    Apr 17 '19 at 0:28

You can swap the buffers between two windows with the following function:

function! SwitchWindow(count) abort
    let l:current_buf = winbufnr(0)
    exe "buffer" . winbufnr(a:count)
    exe a:count . "wincmd w"
    exe "buffer" . l:current_buf
    wincmd p
nnoremap <C-w>x :<C-u>call SwitchWindow(v:count1)<CR>

Now, most often, swapping the buffers will technically act like swapping windows. Because you don't manually keep track of windows, you keep track of which window is holding which buffer. So, the function above will get you by for most situations.


You can use vim-windowswap, gives you the ability to swap two panes in vim using <leader>ww to select and then pressing it again to swap it with the pan you wanna replace it with

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.