views:

260

answers:

3

Assume I've got some arbitrary layout of splits in vim.

____________________
| one       | two  |
|           |      |
|           |______|
|           | three|
|           |      |
|___________|______|

Is there a way to swap one and two and maintain the same layout? It's simple in this example, but I'm looking for a solution that will help for more complex layouts.

UPDATE:
I guess I should be more clear. My previous example was a simplification of the actual use-case. With an actual instance: alt text

How could I swap any two of those splits, maintaining the same layout?

A: 

Take a look at :h ctrl-w_ctrl-x and/or :h ctrl-w_ctrl-r. These commands allow you to exchange or rotate windows in the current layout.

Edit: Actually, this will not work in this situation because it only will swap in the current column or row. You could instead go to each of the windows and select the target buffer, but that's pretty verbose.

Randy Morris
A: 

Randy's correct in that CTRL-W x doesn't want to swap windows that aren't in the same column/row.

I've found that the CTRL-W HJKL keys are most useful when manipulating windows. They will force your current window out of its current location and tell it to occupy the entire edge indicated by the direction of the key you press. See :help window-moving for more details.

For your example above, if you start in window "one", this does what you want:

CTRL-W K   # moves window "one" to be topmost,
           #   stacking "one", "two", "three" top to bottom
CTRL-W j   # moves cursor to window "two"
CTRL-W H   # moves window "two" to be leftmost,
           #   leaving "one" and "three" split at right

For convenience, you can assign the sequences you need to key mappings (see :help mapping).

MikeSep
+3  A: 

Starting with this:

____________________
| one       | two  |
|           |      |
|           |______|
|           | three|
|           |      |
|___________|______|

Make 'three' the active window, then issue the command ctrl-w J. This moves the current window to fill the bottom of the screen, leaving you with:

____________________
| one       | two  |
|           |      |
|___________|______|
| three            |
|                  |
|__________________|

Now make either 'one' or 'two' the active window, then issue the command ctrl-w r. This 'rotates' the windows in the current row, leaving you with:

____________________
| two       | one  |
|           |      |
|___________|______|
| three            |
|                  |
|__________________|

Now make 'two' the active window, and issue the command ctrl-w H. This moves the current window to fill the left of the screen, leaving you with:

____________________
| two       | one  |
|           |      |
|           |______|
|           | three|
|           |      |
|___________|______|

As you can see, the manouevre is a bit of a shuffle. With 3 windows, it's a bit like one of those 'tile game' puzzles. I don't recommand trying this if you have 4 or more windows - you'd be better off closing them then opening them again in the desired positions.

I made a screencast demonstrating how to work with split windows in Vim.

nelstrom
You went the extra mile making a screencast, nelstrom, but it's not really what I was looking for. I can work with splits with the basic movement commands, but what I'm curious about is if there's a way to swap split locations in a layout of arbitrary complexity.
wes