23

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.

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

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.

10

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).

2

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

  • 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 Bhat Feb 4 '15 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 '15 at 5:42
2

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>
  • 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 '15 at 21:42
  • Why the downvote? – John Schmitt Feb 18 '15 at 21:51
  • Not me. Probably whoever flagged it. – muru Feb 18 '15 at 21:59
  • copy-and-paste from SO isn't much more useful than linking – alxndr Feb 18 '15 at 23:30
  • I agree. What would be the most useful thing I could do? – John Schmitt Feb 18 '15 at 23:44

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.