0

I use Vim from the CLI and use tabs rather than buffers (as I understand the terminology). I may or may not open multiple files using the -p option, but end up with a few tabs open at once. If I remember to do so, I'll run :mkview on a tab when I've made changes that matter (usually folding). When I open a file I do :loadview to get back to what I had. The au BufWinEnter ?* silent loadview command, if understood, will load the view whenever I open the file, saving my keystrokes. Similarly au BufWinLeave ?* mkview should save the status when I close the file. However, if I have not manually done :mkview on a tab with folding changes and exit with :qa only the folding for the current tab is saved, all others remain the way they were at the last manual :mkview.

The only clue to the issue, and not a solution, is in the docs on h: BufWinLeave which has:

When exiting and v:dying is 2 or more this event is not triggered.

It is possible that using sessions might help, except that I seldom have the same set of files open that I will want to open the next time.

So, how can I get all open tabs to save their state when I do a full exit?

8
  • 2
    Did you look at :h v:dying. That's not going to be the problem under any normal circumstances. Personally I'd try various other "Leave" and "Unload" events for buffers and tabs (after reading the docs for each to make sure there are no reasons to disqualify it)...see if I had any better luck with those.
    – B Layer
    Jun 12, 2021 at 1:16
  • Ok, v:dying isn't the source of the problem. I'm new enough to power-using Vim that the solution escapes me. The help for :tabnew shows the sequence of triggered events when called. Is there a similar sequence somewhere for the events triggered for :qa or :xa?
    – Chindraba
    Jun 12, 2021 at 1:38
  • @BLayer In chasing down events, I think the data layout is vim can have tab-pages, which can have windows, which can display a buffer. Correct?
    – Chindraba
    Jun 12, 2021 at 1:41
  • 1
    I wish I had more references, but auto-creating and loading views can have unwanted and hard-to-debug effects when some preference saved from the view invisibly overwrites one you’re trying to use.
    – D. Ben Knoble
    Jun 12, 2021 at 11:48
  • 1
    I think :tabdo windo mkview or similar would work? I think I’ve seen people manage a view for each window or buffer with two short autocommands, but idk how it interacts with tabs? If you want to try sessions, which I think are superior, you’ll probably like tpope/obsession.
    – D. Ben Knoble
    Jun 12, 2021 at 16:23

1 Answer 1

3

As discussed in the comments,

autocmd ExitPre ?* tabdo windo mkview

should do it. The event VimLeavePre may be better.

I confess I’m not sure why the pattern is ?* on this one.

Also, don’t forget to augroup.

3
  • The pattern * matches anything, including nothing. Using ?* matches anything which is something. The end result is the the command will not be executed when Vim is started without a file. With the pattern * an error, E32: Ni file name is generated when opening Vim w/o a file. Other than habit the ? is pointless in the exit pattern, however. Some testing turned out to show that VimLeavePre is a better trigger event, in my work-flow anyway.
    – Chindraba
    Jun 13, 2021 at 0:23
  • @Chindraba cool, thanks! I’ll edit more in when I get to a keyboard :)
    – D. Ben Knoble
    Jun 13, 2021 at 0:57
  • ExitPre does work, it just makes some things not fit my habits as well as VimLeavePre does.
    – Chindraba
    Jun 13, 2021 at 2:50

Your Answer

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

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