39

Consider a case with multiple splits in a window. vim allows quickly "maximizing" the current split to take up the whole window via <C-W> o.

Is there a way get vim to "remember" the splits that existed before and revert to this state after executing <C-W> o?

EDIT: To clarify, I'm looking for a way to do this without the use of any third-party plugins.

:help CTRL-W_o

3
  • 1
    I found this answer over at SO relevant, but I'd like to have this functionality without a plugin if possible.
    – Bhargav
    Feb 4, 2015 at 4:15
  • 5
    Please include requirements like that in the question itself, instead of in a comment.
    – muru
    Feb 4, 2015 at 8:00
  • Is this question considered a duplicate if it's on another stackexchange site? stackoverflow.com/q/13194428/1698426 Feb 18, 2015 at 21:55

6 Answers 6

18

The simplest way to do this is probably with session files, which are vim scripts (that vim can automatically produce for you) that will restore a previous editing environment.

You can create a new session file using :mksession <filename>. You can restore that session by executing the generated session script, :source <filename>.

Thus, you could save the current session prior to maximizing a window and restore it when you want to "unmaximize" the window.

Using this, you could remap the appropriate windowing commands to save a session beforehand, and add a new mapping for one to "undo" the last windowing command by restoring the session. For example:

  • nnoremap <C-w>o :mksession! ~/session.vim<CR>:wincmd o<CR> and so on for all the windowing commands you want to support (see :help CTRL-W, there's a lot). The ! prevents errors relating to overwriting an existing session file.
  • nnoremap <C-w>u :source ~/session.vim<CR> to "undo" the last windowing command.

If you wanted to be particularly robust, you could extend this via functions to save the session files in a stack and pop them off to get a full undo chain, although at that point it's probably more efficient to write a bunch of functions to track the window command history in memory... at which point you are likely duplicating the plugins that do this sort of thing already.

13

Another option could be to open your current window in a new tab, then simply close the tab when you're done.

The :split (or :sp for short) command, without an argument, has the effect of opening a new split with the same buffer as the current split. The :tab command can run many window-related commands, changing them to use a tab instead. Combining these together gives :tab sp, which will open a new tab with the current buffer.

Afterward, you can close the new tab with :q or :tabclose (:tabc for short).

5

What you are describing sounds exactly like the feature that goes by the name of "zooming". There is already a great answer on SO that does it very well.

EDIT: Explicit answer, copied and pasted from SO. I put the snippet below in my .vimrc and then invoke

:ZoomToggle

in my vim command prompt.

" Zoom / Restore window.
function! s:ZoomToggle() abort
    if exists('t:zoomed') && t:zoomed
        execute t:zoom_winrestcmd
        let t:zoomed = 0
    else
        let t:zoom_winrestcmd = winrestcmd()
        resize
        vertical resize
        let t:zoomed = 1
    endif
endfunction
command! ZoomToggle call s:ZoomToggle()
nnoremap <silent> <C-A> :ZoomToggle<CR>
5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – muru
    Feb 18, 2015 at 21:42
  • Why the downvote? Feb 18, 2015 at 21:51
  • Not me. Probably whoever flagged it.
    – muru
    Feb 18, 2015 at 21:59
  • copy-and-paste from SO isn't much more useful than linking
    – alxndr
    Feb 18, 2015 at 23:30
  • I agree. What would be the most useful thing I could do? Feb 18, 2015 at 23:44
3

I use the ZoomWin plugin (https://github.com/itspriddle/ZoomWin) for exactly this scenario. Pressing CtrlW-O a second time restores your window layout.

2
  • 1
    thanks for the suggestion, but as I did note in my question, I'd like to know if this can be done without a plugin.
    – Bhargav
    Feb 4, 2015 at 7:28
  • 1
    This plugin was slow for me for some reason when zooming out. Anyone else having the same issue?
    – shriek
    Feb 19, 2015 at 5:42
1

This post is old, the accepted answer doesn't concern about terminal buffer.

The accepted answer use session to do the trick, but session can't completely restore terminal buffer:

                            *terminal-session*
A terminal window will be restored when using a session file, if possible and
wanted.

If "terminal" was removed from 'sessionoptions' then no terminal windows will
be restored.

If the job in the terminal was finished the window will not be restored.
...

Tommcdo's answer still works, <c-w>:tab split works well on terminal buffer . It deserves a map:

nnoremap <c-w><space> :tab split<cr>
tnoremap <c-w><space> <c-w>:tab split<cr>
0

I found a nice plug call vim-zoom which zooms into a window using the default key stroke Ctrl+w+m. To unzoom and restore the last window configuration repeat the key stroke.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.