I recorded info of terminal buffers in a dict, and want remove bufnr of closed ones.

Can you tell me how to start a terminal such that call a function on exit, eg,

func! myterm#remove(bufnr):
    call remove(s:terms, a:bufnr)
  • you probably need to use term_start() and use the callback options for this specific use case. Nov 27, 2019 at 11:38

1 Answer 1


The solution is using job-exit_cb handler, (see :help job-exit_cb)

A working example is

func! g:Exit_cb(job, exit_code)
    echo term_getjob(2)
    let pid = matchstr(a:job, '^\%(process \)\zs\d\+')
    echom "job " . pid . " finished with exit_code " . a:exit_code

call term_start('bash', {'exit_cb': function('g:Exit_cb')})

Run above code, then close the newly created terminal, you will see something like below

job 61392 finished with exit_code 0

be echoed at the bottom, you can also verify with :mess command. (since `:echom is used)

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.