vimtricks.wiki Concise Vim tricks, one at a time.

How do I swap the contents of two split windows in Vim?

Answer

<C-w>x

Explanation

How it works

The <C-w>x command exchanges the current window with the next one. This swaps the buffer displayed in each window without changing the window layout itself. The windows stay in the same position and size, but the files shown in them trade places.

If you have more than two windows, <C-w>x swaps the current window with the one that comes next in window order (typically the window below or to the right). You can also use a count: 2<C-w>x swaps the current window with the window two positions ahead.

The related command <C-w>r (rotate) shifts all windows down or right by one position, which is useful when you want to cycle the order of three or more windows.

Example

Suppose you have two files open in a vertical split:

+----------+----------+
| main.py  | test.py  |
+----------+----------+

With your cursor in the main.py window, press <C-w>x to swap:

+----------+----------+
| test.py  | main.py  |
+----------+----------+

This is particularly useful when you open a split and the file ends up on the wrong side. Instead of closing and re-opening with a different split command, just press <C-w>x to quickly swap them into the arrangement you want.

For moving windows to entirely different positions (top, bottom, left, right), use <C-w>H, <C-w>J, <C-w>K, or <C-w>L instead.

Next

How do you yank a single word into a named register?