3

I was looking at the documentation for the 'complete' option, and I found it a bit confusing:

.   scan the current buffer ('wrapscan' is ignored)
w   scan buffers from other windows
b   scan other loaded buffers that are in the buffer list
u   scan the unloaded buffers that are in the buffer list
U   scan the buffers that are not in the buffer list
k   scan the files given with the 'dictionary' option
kspell  use the currently active spell checking |spell|
k{dict} scan the file {dict}.  Several "k" flags can be given, patterns are valid too.  For example:  
        :set cpt=k/usr/dict/*,k~/spanish
s   scan the files given with the 'thesaurus' option
s{tsr}  scan the file {tsr}.  Several "s" flags can be given, patterns are valid too.
i   scan current and included files
d   scan current and included files for defined name or macro |i_CTRL-X_CTRL-D|
]   tag completion
t   same as "]"

What is the difference between b, u and U? I thought as soon as you did either :e file/path or :badd file/path, everything gets "loaded" into memory for editing..

0

2 Answers 2

6

The situation with buffers is quite complicated. Use :ls (of :buffers) to inspect them.

There can be the following states:

  1. Active buffer --- both loaded into RAM and shown in (at least one) window. This is what you normally got after :e filename, :buffer N and so on. (flagged "a" by :ls; also when it's in active window, it's flagged by "%")

  2. Hidden buffer --- fully loaded but nowhere shown, except the buffer list. This is when you call bufload() to load a new buffer without showing it, or :hide an existing window/buffer, or do :quit while set hidden or setlocal bufhidden=hide etc. (flagged "h" by :ls)

  3. Unloaded buffer --- "the data stream" is unloaded from RAM, but all other stuff, such as local variables, options, bookmarks etc., is still preserved. This is what you get after :bunload, or (if set nohidden) when switching buffers by CTRL^, or do :quit! etc. (no special flag in :ls, except maybe #).

  4. Unlisted buffer --- this is just a special flag which is honoured by :ls command: unlisted buffers are not shown unless :ls! is specifed. (flagged "u" by :ls!)

In fact, any buffer has two independent flags: loaded and listed. "Loaded and nolisted" is a typical value for special buffers such as "help", so they are visible but not normally shown by :ls; "unloaded and nolisted" is set when a buffer is deleted.

  1. Deleted buffer --- this is both unloaded and unlisted buffer. "The data stream" is unloaded from RAM, and all buffer-local variables and options are erased. However, all local bookmarks are still intact alongside with "filename" and "buffer number". This is what you get after :bdelete. Also the same fate awaits for all "nofile" and "unnamed" buffers - they are not just unloaded but also deleted (also flagged "u" by :ls!, as they all are unlisted too).

  2. Wiped buffer --- this is a full extermination, no mercy, no trace in history. Done by :bwipeout.

8
  • For buffers, what are "local variables, options, bookmarks etc."? Could you provide a short explanation; or where I can read about this? A Google search gives vague results.
    – herophant
    Commented Oct 12, 2019 at 17:29
  • @herophant Please, forget about google and start to use the help system: :h buffer-variable, :h local-options and so on.
    – Matt
    Commented Oct 12, 2019 at 17:40
  • 2
    @NicholasCousar Buffer = data + metadata. How much of metadata is freed or kept depends on buftype and operation. Only wipeout destroys everything including "name" and "number".
    – Matt
    Commented Sep 1, 2020 at 4:37
  • 1
    @job_start No, in Vim speak it is loaded. You got it wrong. "Loaded" = "contents is not null".
    – Matt
    Commented Jan 24, 2023 at 17:17
  • 1
    You can go on and inspect C code, if you really wish. This is what OpenSource stands for. There's buffer struct that has contents field, that may be null, or it may be line array, that may be empty (or it has single empty "virtual" line - I don't remember all the details and I don't need to). Kind of that.
    – Matt
    Commented Jan 24, 2023 at 18:40
6

A buffer is listed if it's in the buffer list, you can see it in :h :buffers , you can refer to it with :h :buffer , :h :bnext , etc . It's controled by setting :h 'buflisted' .

A buffer is loaded if the corresponding file is loaded into memory.

Not all buffers should be listed, e.g. help type buffer is not listed, as no one wants to refer to it with :h :buffer .

Not all buffers should be loaded, as load is not free, it cost time and memory, it doesn't make sense to load a buffer that you might not use at all.

:e file/path create a listed and loaded buffer.

:badd file/path create a listed but not loaded buffer.

:args a b c d creates 4 buffers, all of them listed, but only a is loaded.

:bunload unload buffer.

:bdelete unload buffer, remove it in the buffer list, clear options, variables, maps, abbreviations.

:bwipe unload buffer, remove it in the buffer list, wipe everything.

You can check buffer info with :h getbufinfo() .

You can also check unlisted buffer with :buffers!.

2
  • What does :b file/path do? I'm guessing it edits file/path since it shows up on the terminal screen. Say you only have one window and you are editing file/path. You execute :b file/path2. Now you certainly have file/path2 loaded since you are editing it, but what happened to file/path? Has it been unloaded and that memory freed?
    – herophant
    Commented Oct 12, 2019 at 4:57
  • Original buffer (file/path) will be :h abondon , it's unloaded only if 'hidden' is off.
    – dedowsdi
    Commented Oct 12, 2019 at 5:28

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.