21

When I do vimdiff file2 file1, file2 naturally goes on the left and file1 on the right.

Sometimes I find that I put them the wrong way round, so I'd like to be able to switch them round without leaving Vim. Is that possible?

22

You can use Ctrlw-x. From :he CTRL-W_x:

CTRL-W x                                            CTRL-W_x CTRL-W_CTRL-X
CTRL-W CTRL-X   Without count: Exchange current window with next one.  If there
                is no next window, exchange with previous window.
                With count: Exchange current window with Nth window (first
                window is 1).  The cursor is put in the other window.
                When vertical and horizontal window splits are mixed, the
                exchange is only done in the row or column of windows that the
                current window is in.
10

As you would switch any other window, <c-w>x or <c-w>r are two options.

Having only two windows opended <c-w>k will switch them and leave the cursor in the window where it was before the switch (i.e. if before the switch the focused window is on the left, after the switch it will be on the left).

<c-w>x will switch the windows and move the cursor in the switched window (i.e. the focused window remain on the left if it was on the left).

See :help window-moving for more command to move windows aroud.

3

I find the following commands much more intuitive than Ctrlw-x.

:help CTRL-W_K

The following commands can be used to change the window layout.  For example,
when there are two vertically split windows, CTRL-W K will change that in
horizontally split windows.  CTRL-W H does it the other way around.

                        *CTRL-W_K*
CTRL-W K    Move the current window to be at the very top, using the full
        width of the screen.  This works like closing the current
        window and then creating another one with ":topleft split",
        except that the current window contents is used for the new
        window.

                        *CTRL-W_J*
CTRL-W J    Move the current window to be at the very bottom, using the
        full width of the screen.  This works like closing the current
        window and then creating another one with ":botright split",
        except that the current window contents is used for the new
        window.

                        *CTRL-W_H*
CTRL-W H    Move the current window to be at the far left, using the
        full height of the screen.  This works like closing the
        current window and then creating another one with
        ":vert topleft split", except that the current window contents
        is used for the new window.
        {not available when compiled without the |+vertsplit| feature}

                        *CTRL-W_L*
CTRL-W L    Move the current window to be at the far right, using the full
        height of the screen.  This works like closing the
        current window and then creating another one with
        ":vert botright split", except that the current window
        contents is used for the new window.
        {not available when compiled without the |+vertsplit| feature}

These commands can change the window's size, however, so be aware of that.

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.