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.
I guess I should be more clear. My previous example was a simplification of the actual use-case. With an actual instance:
How could I swap any two of those splits, maintaining the same layout?
I put sgriffin's solution in a Vim plugin you can install with ease! Install it with your favorite plugin manager and give it a try: WindowSwap.vim
To move between splits first press Ctrl-w (I remember this by Control Window, I'm not sure what the official mnemonic is) Then press a directional key to move the cursor to the split you're interested in. Directional key could be the arrows or my preferred home row method.
After Vim rotates the windows, the cursor remains in the window from which the rotate command executed; thus, the cursor moves with the window. CTRL-Wx and CTRL-WCTRL-X let you exchange two windows in a row or column of windows.
To split the vim screen horizontally, or open a new workspace at the bottom of the active selection, press Ctrl + w , followed by the letter 's' . In the example below, the left section has been split into two workspaces. To navigate to the bottom section hit Ctrl + w , followed by the letter 'j' .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With