0

I've been looking through :help autocmd-events for an event that is going to be ran when a file is closed, but I got pretty confused, seeing there were many events that sound like they might do their job, but also that might not do it completely, for example: BufUnload, BufWinLeave, BufLeave, but their descriptions confuse me a bit.

What I am looking for is an autocmd that is going to be run on any writable file, excepting some filetypes that appear in a table, before the respective file is closed.

What autocmd event should I be using for this situation, and how would the pattern I mentioned look like? I'll be writing my config in Lua, but Vimscript solutions are welcome too, since I can translate them for myself later.

7
  • I suppose you have to use BufUnload and test in your function if the corresponding buffer <afile> filetype should be checked against your list. Jun 23 at 9:48
  • 2
    I think you're going to have define "closed" a bit more carefully. As Matt perhaps ungraciously points out, vim doesn't quite have this concept. But vim has buffers, that can be hidden, unloaded, deleted, and wiped (assuming they are loaded to begin with), and windows, which can be entered and left. And then there's quitting vim altogether. So it really depends on when you want to do the thing.
    – D. Ben Knoble
    Jun 23 at 14:36
  • @D.BenKnoble Well, what I'm going to make that autocmd do is have it modify the file content just before closing the file, including Vim quitting. What I mean with closing.. uh, just when the file gets closed? For example when the file is no longer in my tab bar? Is that a good explanation? I kind of figured that possibly Vim doesn't know that, since I think that if it would've, there would've been a more straightforward autocmd event for it
    – Andy3153
    Jun 23 at 16:25
  • 1
    This is a bad explanation, as it only shows that you have wrong assumptions. One thing is on screen, another one is in RAM, and another one is on disk. There is no single event that applies to all these three things at once. There could be some commands that affect them all. But event is something different. It doesn't say what command is executing, but it says what object has its state changed.
    – Matt
    Jun 23 at 18:05
  • 1
    @Andy3153 what tab bar? By default, the tabline (:help tabline) shows open tab pages. Perhaps you have a plugin that uses it to display buffers?
    – D. Ben Knoble
    Jun 23 at 20:10

1 Answer 1

1
  1. Read :h BufWinLeave carefully
  2. Read :h BufUnload carefully
  3. Read :h 'hidden' carefully
  4. ...
  5. PROFIT

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.