I've configured Vim to look like this:

| N  |              Code             |
| E  |                               |
| R  |                               |
| D  |                               |
| T  |                               |
| r  |-------------------------------|
| e  |              REPL             |
| e  |                               |

Every element is in it's own buffer. Is it possible to delete the REPL buffer only when I quit the code buffer? If I open and close help, for example, the REPL should stay but when I close the code buffer, the REPL buffer should close as well.
This is what I have now:

let s:code_repl_nrs = {} " A map of (code buffer number) - (REPL buffer number)
let s:last_leaved_buffer_nr = -1 " The number of the last leaved buffer

autocmd BufLeave,BufDelete * let s:last_leaved_buffer_nr = bufnr('%')
autocmd BufUnload * call <SID>StopREPL(s:last_leaved_buffer_nr) " Stop the REPL when closing the code buffer

"" Stops a REPL given the number of the current buffer
function! s:StopREPL(buffer_nr)
    let l:repl_nr = get(s:code_repl_nrs, a:buffer_nr . '', -1) " Get the number of the REPL buffer
    if l:repl_nr != -1
        exec l:repl_nr . 'bdelete!'

When I use :h, the REPL closes. How can I prevent this?

  • First you need to define the mapping according to some rule. For example, :repl command record the current buffer number and then maps it to the number of REPL buffer to be created then. And then is what you have written above that handle the BufDelete event. So what is your problem now?
    – ZhiyuanLck
    Jan 4 at 14:50
  • The problem is that the REPL closes when I open help. I want to "link" the buffers - when one is closed, the other to be closed as well.
    – Loading BG
    Jan 4 at 14:56
  • You call StopREPL on all BufUnload events… that includes the help buffer closing, no?
    – D. Ben Knoble
    Jan 4 at 19:51
  • What do you mean by "all"? Isn't the * enough for all BufUnload events? Also the REPL closes when the help buffer opens for some reason. I tried to examine that behavior by making a bunch of autocmds which echoed a message but I couldn't understand which buffer is unloaded.
    – Loading BG
    Jan 4 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.