1

Suppose I have unloaded a buffer and deleted it from the buffer list using :bd.

Now, it won't show in the buffer list (:ls) anymore, unless ! is specified in the command: :ls!

How can I add the unloaded buffer back into the buffer list?

3

The command :bdelete N tends to be misleading. What it actually does is:

1) free (i.e. unload) the N-th buffer's contents from RAM (while keeping its number, name, options etc.)

2) remove it from the list of "normal" open buffers (i.e. set nolisted option)

But, in fact, these two effects could be also independent one from another and there's no matching "bundelete" command to switch both on without other "side-effects". What you can do is

1) :h bufload(), :h bufloaded(), :h :bunload to load/unload the buffer's contents only

2) :call setbufvar(N, '&buflisted', zero_or_one) to switch "listed" flag only

3) Edit the buffer which also (obviously) loads it and makes it "listed", e.g. :edit #N or :split #N etc. However note that :buffer N differs in that it does not set listed flag (it is explicitly mentioned in :h :buffer).

| improve this answer | |
  • thank you. Why doesn’t :edit N the # work? By “open buffer” you mean a loaded buffer present in RAM, so unloading is the same as closing? – Shuzheng May 11 at 4:35
  • @Shuzheng edit N means edit buffer named "N", edit #N means edit buffer number N; closing does not always mean unloading: if there's other window showing this buffer, or set hidden, or setlocal bufhide=hidden then the buffer could still be loaded (it depends on exact command to close it: say, ZQ always unloads, but :quit respects 'hidden' option, etc.). – Matt May 11 at 4:44
  • @Shuzheng Strictly speaking, you can "close" only windows, while buffers are "unloaded". If you unload a buffer, all its windows are closed too; if you close all windows showing the buffer then the buffer may be unloaded, but it can also remain loaded. In the latter case it's called "hidden". – Matt May 11 at 4:53
  • What does “open” mean then? That the buffer is loaded? – Shuzheng May 11 at 4:53
  • @Shuzheng Well, that's not really a good word, I admit. I rather meant "listed" buffers. – Matt May 11 at 4:55
3

All you need to do is set the 'buflisted' option on the buffer again. So, running :setlocal buflisted on the buffer itself would work.

If you don't have the buffer opened, but you have the buffer id, you can use the setbufvar() function, using the &buflisted syntax to set the value of the option on the buffer.

For example, for buffer 7, you can use:

:call setbufvar(7, '&buflisted', 1)

You can confirm that it will now show in the :ls output after this command is executed.

| improve this answer | |
2

(In the case where you :bwipeoutd the buffer, so it's really gone.)

By :editing a file (or :spliting it, or any other command that brings it into a window/tabpage), you'll automatically add it to the buffer list.

If you want to add it to the buffer list without summoning it up for editing immediately, you have to do a bit of a dance:

let bufnr = bufadd('file')
call bufload(bufnr)

This loads the buffer after adding it, but it still won't show without :ls! because it is unlisted. You can use filbranden's answer to toggle that variable.

| improve this answer | |

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.