9

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)
the_velour_fog
  • 3,335
  • 3
  • 22
  • 40
  • 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 06 '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 06 '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 07 '16 at 00: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 07 '16 at 00:14
  • obsession will save help windows if you add help to sessionoptions some time before saving the session: set sessionoptions+=help. – Sato Katsura Jul 07 '16 at 06:22
  • @SatoKatsura yes I think the problems are caused by the fact I am using taboo.vim and Obsession together - I think by themselves they work ok, but together there seems to be some conflict. – the_velour_fog Jul 07 '16 at 06:30
  • As far as I can tell taboo doesn't touch sessionoptions. – Sato Katsura Jul 07 '16 at 06:35
  • @SatoKatsura oh yea taboo touches sessionoptions! well technically you need to modify sessionoptions yourself to get your taboo tabs restored i.e. set sessionooptions+=globals,tabpages they suggest this snippet on the taboo github readme this is a bad snippet because it doesnt check what sessionoptions value it before it changes it. also tim pope has been making changes to sessionoptions in obsession – the_velour_fog Jul 07 '16 at 06:41
  • taboo uses sessionoptions. It doesn't change it. Have you actually tried my suggestion above before arguing about it? – Sato Katsura Jul 07 '16 at 06:44
  • @SatoKatsura no point. help is already in my sessionoptions with or without :Obsession . also there are more problems than just missing tabpages with help windows. there are tab labels that are attached to the wrong tabs. I need to spend some time testing – the_velour_fog Jul 07 '16 at 06:48

1 Answers1

9

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
the_velour_fog
  • 3,335
  • 3
  • 22
  • 40