2

Situation: I've typed this:

vim foo.js
:terminal

Now I have one split, a file editing buffer and a terminal.

The good: I can quit by closing the terminal first, then the file buffer.

The bad: But if I close the file buffer, and then the terminal, vim doesn't exit, but instead creates a new buffer editing the file, which I've just closed.

I find this distracting and confusing. I don't want it to open up foo.js again.

If that doesn't seem like a bug/misfeature, consider this use-case:

  • I open up foo.js
  • :terminal
  • close foo.js
  • open up foo.js in another instance of vim
  • in the original vim instance, close the :terminal

In this case when the original vim instance tries to open foo.js (instead of quitting) I get a large error screen about "Another program may be editing the same file."

How do I make vim quit when I close the last buffer, even if it's a :terminal?

2 Answers 2

2

Thanks to @Matt I now know that if I close the terminal buffer myself instead of having the 'term_finish':'close' argument of term_start() do it, then it doesn't do the weird behavior of sometimes opening a different file.

So I've 'term_finish':'close' that with an exit_cb that closes the buffer.

Here's what I use in full:

function! JW_on_term_exit(a, b)
    normal q!
endfunction
nnoremap <silent> <Bslash> :below call term_start('env TERM=st-256color zsh', { 'exit_cb': 'JW_on_term_exit', 'term_name': 'zsh', 'norestore': 1 })<Return>
1

You can create a mapping which will close the terminal buffer directly. Then Vim will not open an additional empty buffer and will exit.

For simplicity I assume that the terminal is running shell, so Ctrl-D exits it normally.

tnoremap <silent><C-D> <C-D><C-\><C-N>ZQ
1
  • Thanks! This is an interesting idea. Unfortunately I do sometimes press ctrl-D for things other than closing my terminal. This technique helped me think of a full solution though!
    – JasonWoof
    Commented Sep 1, 2019 at 20:58

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.