8

When I am working in vim, I often have a split, or splits, open containing a help files.
If I close other windows using :q such that vim would be left in a state with only help windows open, vim closes the entire editor, which causes me to lose my position in the help pages. e.g. Say I have this layout

  tab1 (this is the only tab open)
+------------------+-------------+-----------------+
|                  |             |                 |
|                  |:h usr_26.txt|                 |
|  A normal file,  |             |    :h change.txt|
|  containing code |section 26.2 |                 |
|  e.g.            |line 47      |    line 272     |
|                  |             |                 |
|  app_db.sql      |             |                 |
|                  |             |                 |
|                  |             |                 |
|                  |             |                 |
|                  |             |                 |
|                  |             |                 |
|                  |             |                 |
+------------------+-------------+-----------------+

If I were to enter the window containing app_db.sql an issue the :q command, surprisingly the entire editor would be lost.
It often takes me a long time - up to 15-20mins - of searching the help to finally discover that help content, so even though it's not exactly lost work, it's definitely a lot of time lost when this happens.
How can I prevent vim closing the editor itself when I close a window with only help windows open?

Possible solutions I've considered

  • Simply don't close the last window when there are help windows open.
    • the problem with this is I am not paying attention to what's in other windows when I close the current window.
  • Use mksession
    • this requires that you know ahead of time that you need to :mksession and the editor closing always occurs unexpectedly.
  • Use a plugin like vim obsession which constantly update a Session.vim file.
    • I've currently paused using vim-obsession because of (ironically), the Session.vim files vim obsession makes often don't restore tabs containing help pages, but the Session.vim files generated by :mksession do restore the tabs with help windows. (Also another issue with tab labels getting mis-matched with the tabs themselves)
  • There may be unintended side-effects, but you could try an autocmd watching the QuitPre event which would give the focus back to the previous window before closing the current one: autocmd QuitPre * wincmd p – user9433424 Jul 6 '16 at 23:46
  • @user9433424 nice, that works! thanks. I'll add to my ~/.vimrc and keep an eye on any weird side-effects – the_velour_fog Jul 6 '16 at 23:49
  • @user9433424 actually its not working in all cases, possibly when the last window open is not marked as the previous window, i.e. the wincmd p is not working because p is null maybe. I will have to test a bit later when i get time – the_velour_fog Jul 7 '16 at 0:02
  • Yes you're right sorry, I've just realised it didn't work when there were just 2 windows, and you were closing the help window (then it quits Vim). Maybe someone will have another working solution. – user9433424 Jul 7 '16 at 0:14
  • obsession will save help windows if you add help to sessionoptions some time before saving the session: set sessionoptions+=help. – Sato Katsura Jul 7 '16 at 6:22
7

using the command :clo[se] instead of :q seems to have solved the problem for me.
I.e. if I issue :clo with the above window layout, it causes vim to throw

E444: Cannot close last window.

In fact it seems :close was intended for this situation, i.e. to prevent accidental closing of the editor when all you wanted to do was close a window (or tab)

from :h windows.txt

:clo[se][!]
:{count}clo[se][!]
CTRL-W c                    *CTRL-W_c* *:clo* *:close*
        Without {count}: Close the current window.  If {count} is
        given close the {count} window.

        When the 'hidden' option is set, or when the buffer was
        changed and the [!] is used, the buffer becomes hidden (unless
        there is another window editing it).

        When there is only one window in the current tab page and
        there is another tab page, this closes the current tab page.
        |tab-page|.

        This command fails when:            *E444*
        - There is only one window on the screen.
        - When 'hidden' is not set, [!] is not used, the buffer has
          changes, and there is no other window on this buffer.
        Changes to the buffer are not written and won't get lost, so

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.