For some reason, the BufDelete autocommand is being executed right after a window is created, instead before, and i think it shouldn't be like that, according to the docs:

Before deleting a buffer from the buffer list. The BufUnload may be called first (if the buffer was loaded). Also used just before a buffer in the buffer list is renamed.

I have a dwm-like window orchestration plugin, the editor start with window number 1, then i open window number 2, change to it, whenever i try closing window number 2 by running the comand :TilerClose, it prints out that the window about to be closed is number 1 (and it should be 2 instead), here's my code:

dwm-like window orchestration plugin:

function! tiler#close_window() abort
    let l:opened_buffers = v:lua.get_opened_buffers_count()
    let l:is_terminal = &buftype == 'terminal'
    let s:winid = win_getid()

    if opened_buffers == 1
    elseif l:is_terminal
        bdelete " unregister_buffer should be executed right before this

    call s:set_master_layout(filter(s:get_master_layout(), 'v:val != s:winid'))
    call tiler#reorder()
    call tiler#select_master()

command! TilerClose call tiler#close_window()

My Lua config:

function _G.unregister_buffer()
    print(vim.fn.winnr()) -- returns 1 instead of 2
    vim.fn.getchar() -- waits for input (prevents the print from being ignored)

vim.cmd("autocmd BufDelete * call v:lua.unregister_buffer()")
  • Since you're only showing us part of the code, it's really anyone's guess what's happening. But the question is confusing: you talk about bufdelete after window creation, which doesn't line up with the title. Note that :bdelete will close any windows showing that buffer, however.
    – D. Ben Knoble
    Dec 16, 2021 at 0:02
  • I tested again on a vim instance running a bare-bones config with just enough code for reproducing the issue, a function, and one BufDelete autocommand, and yet, the same issue is happening, my windows all have different buffers, so that's not a problem, i'm just curious of why the function is not printing the window number about to be closed (i tried with both winnr() and bufwinnr(expand('<abuf>'))), somehow the autocommand is being executed after the window is already closed, printing another unrelated window number Dec 16, 2021 at 0:19

1 Answer 1


This is all written under :h BufDelete. It is called only by the time the buffer is removed from the buffer list (think it as BufUnlisted instead).

So the current window/buffer may already change at this time (first, the buffer is unloaded/the window is closed, and only then BufDelete is triggered). And this is also mentioned in help.

Considering expand-abuf - this is a common beginner's mistake. Expand returns string while bufnr et al. think that all strngs are buffer names, not buffer numbers. Make sure to use str2nr or such.

  • Thanks! the tip regarding str2nr really helped me solve some problems i was having (the tonumber lua function also worked fine for converting strings to numbers) Dec 17, 2021 at 18:24

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.