I often use the :term command in my projects to create new directories or perform other work.

However, if I have multiple windows open, then executing :term from one of them will also close that window, when I exit the shell.

How can I execute :term from the current window and return back to current buffer on exit?

  • Note: it's about Neovim. In Vim :term opens new window by default.
    – Matt
    Commented May 11, 2020 at 6:00
  • Yes, you’re right. Is it possible to “fix” Neovim?
    – Shuzheng
    Commented May 11, 2020 at 6:19
  • 1
    I simply do :new +term
    – Matt
    Commented May 11, 2020 at 6:21
  • "to create new directories" For simple external commands, consider using :!mkdir ... or :silent !mkdir ... or :call system('mkdir '.shellescape(...)), etc. Use of :term is usually only needed when you want a full screen app, a REPL, or when you want to run a background process (long compilation) without blocking the editor. But for simple tasks and short commands there are usually better approaches than :term.
    – filbranden
    Commented May 11, 2020 at 8:57
  • @filbranden, why would I want to use this longer form :call system('mkdir '.shellescape(...))? Isn't !mkdir ... almost always the faster/better alternative?
    – Shuzheng
    Commented May 11, 2020 at 13:20

1 Answer 1


You can add this to your init.vim:

function! s:termclose() abort
  let buf = expand('#')
  if !empty(buf) && buflisted(buf) && bufnr(buf) != bufnr('%')
    execute 'autocmd BufWinLeave <buffer> split' buf

autocmd TermClose *:$SHELL,*:\$SHELL call s:termclose()

(It is recommended to use a group for the auto command).

In Neovim terminal buffers are normal buffer's and when they close, :bdelete is executed, and :bdelete's behaviour is weird if you ask me. Anyway there are plans to definitely fix it

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.