3

I'm looking to be able to close a terminal buffer without unloading it, as I would with a normal buffer and either hidden or bufhidden=hide set. It appears, though, that said options are ignored for terminal buffers. Note that I'm using vim 8.2.

Here's a reproducible example:

vim -u NONE
:set hidden
:vnew regular_buffer
:close
:ls

buffers:

:ls
  1 %a   "[No Name]"                    line 1
  2 #h   "regular_buffer"               line 0

next, create a terminal buffer:

:call term_start('ls', {'term_name': 'terminal_test'})
:ls

and the new buffer list:

:ls
  1 #a   "[No Name]"                    line 1
  2  h   "regular_buffer"               line 0
  3 %aF  "terminal_test [finished]"     line 1

now, close the terminal buffer:

:close

and again show the buffer list:

:ls
  1 %a   "[No Name]"                    line 1
  2 #h   "regular_buffer"               line 0

Trying to reopen the buffer (:b3) confirms it no longer exists. How can I keep my terminal buffer loaded (and in the buffer list) after closing its window?

cmaher
  • 141
  • 5

1 Answers1

4

Terminal buffer is special:

When the job has finished and no changes were made to the buffer: closing the window will wipe out the buffer.

Change buffer type will stop this.

Manual

After job finished, clear buffer type:

setlocal buftype=

This also works:

setlocal modifiable
" make any change, the 1st change will convert terminal buffer to be a normal buffer.

See doc after :h E948 for detail.

Auto

Pass this options to term_start:

{ 'term_finish' : 'open',  'term_opencmd' : 'call setbufvar(%d, "&buftype", "")' }

It will clear buffer type when the job is finished, the %d will be replaced with terminal buffer number.

See :h term_start for detail.

dedowsdi
  • 6,248
  • 1
  • 18
  • 31