If I have windows arranged into four quadrants, as from the following sequence of commands:

  • :tabnew
  • :vsplit
  • :split
  • <C-W>l
  • :split

then entering a command like <C-W>J will cause these to no longer be square; in this case, it will force one window to float at the bottom.

How can I do the opposite—that is, force the windows back into a square formation?

I've read :help window-moving but didn't see anything useful.

  • 1
    I'm a bit unclear as to what you're trying to achieve when you press <C-W>J, which moves "the current window to be at the very bottom, using the full width of the screen," This means the windows will no longer be in a grid formation, by definition. What are you trying to actually do with your windows?
    – Rich
    Feb 18 '15 at 11:26
  • 3
    @Rich I think OP is just exemplifying a situation after which he may want to reset the windows to grid formation. Feb 18 '15 at 18:12
  • @GonçaloRibeiro Correct.
    – wchargin
    Feb 18 '15 at 18:16
  • Though not an answer, if you find yourself hunting for code examples to make your own solution, I recommend checking the fabulous Microviche project. Feb 22 '15 at 18:17

As far as I know, there is no easy way to arrange open splits into a grid of splits. In this case, the easiest way to get back into a grid is to manually rectify the structure:

  1. close the split floating at the top
  2. return to the split taking up half the screen
  3. re-split it with :split <file>

This will return you to a grid. The way to maneuver splits without affecting the window layout is to use [count] Ctrl -W x , which exchanges the current window with the [count] window.

  • I know that I can do this, but there are a couple of downsides. (1) If the window is the only view of a given buffer, and the buffer is dirty, you'll have to :wq or :q!, neither of which is desirable. (2) This will reset any window-specific settings, like the cursor position. (3) This will break any in-progress diffs. Essentially, windows are first-class citizens; destroying them and creating new ones simply isn't sufficient.
    – wchargin
    Feb 18 '15 at 1:42
  • @WChargin Some of that goes away if you set vim to allow hidden buffers. But yeah...
    – derobert
    Feb 18 '15 at 16:54
  • 2
    Instead of closing the window and splitting the buffer to the desired location, split a new window where you want it to go, exchange it with the window you want to move, then close the (moved) new window.
    – tommcdo
    Feb 19 '15 at 2:02

You could save your favourite layout using the winsaveview command. Here I am mapping it to \sv for convenience:

nnoremap <Leader>sv :let g:myLayout = winsaveview()<CR>

Be sure to run that before you break your layout! ;-)

Now if you ever accidentally break your layout, you can restore it:

nnoremap <Leader>rv :call winrestview(g:myLayout)<CR>

The default keys to re-arrange windows only work within their local split group:

CTRL-W x   Exchange current window with next (in group)
CTRL-W r   Rotate windows down/right (in group)

Or they break the window out to the top group:

CTRL-W  H | J | K | L   Move window to left/top/bottom/right-most edge

These are inadequate when you want to move the buffer into one of the other split groups.

But on such occasions, the WindowSwap plugin can help. It lets you to swap the buffers of any two arbitrary windows.

<leader> yw   "Yank window": Yank the current window

<leader> pw   "Paste window": Swap the current window with the yanked window

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.