Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NeoVim make terminal window automatically close when program exits

Tags:

neovim

The vim version has a ++close option that automatically closes the terminal when the job terminates, but since the NeoVim terminal came first it doesn't have that feature.

I need this because i am trying to run programs directly from vim, but i regularly make curses applications which the popup window you get from :!python % can't handle, so i need the terminal window.

I am using NeoVim on arch linux if that's relevant.

like image 526
j307 Avatar asked Mar 10 '26 12:03

j307


1 Answers

You can accomplish this by using an autocmd on the TermClose event. From the neovim manual (:help terminal):

autocmd TermClose * if !v:event.status | exe 'bdelete! '..expand('<abuf>') | endif

I didn't care about the exit status, so for my personal configuration I simplified it to

autocmd TermClose * execute 'close!'

Finally, since I am using lua in ~/.config/nvim/init.lua with neovim, I translated it to lua as follows:

vim.api.nvim_create_autocmd("TermClose", {
    callback = function()
       vim.cmd("close")
    end
})
like image 181
Razzi Abuissa Avatar answered Mar 12 '26 20:03

Razzi Abuissa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!