By using the following code...


I can open a terminal with a certain command. What I want to do is run a Lua function from a plugin upon exiting the terminal.

Pseudo Code:

vim.fn.termopen("foo", run "lua require('my-plugin').someFunction()" upon exiting)

Is this possible?


After a bit of research, I found that there are two autocommands I can use that suit my needs

au TermClose * lua require('my-plugin').someFunction()


au TermLeave * lua require('my-plugin').someFunction()

The first one works better for me because I only want to run my function once the terminal is killed/closed. The second one is for if you want to run a function once the user switches from the terminal to something else.


In regular vim, the API would be term_start() with the exit_cb option set:

function Exit(job, exit_status) abort
  echom 'goodbye world'
call term_start('echo hello world', #{exit_cb: 'Exit'})

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.