Reputation: 429
:ls
gives the list of open buffers. I was of the view that one window can hold just one buffer, and the current buffer = current file that is being edited. What exactly are other "open buffers" if I have exactly one window open at a time?
How is :ls
different from :browse oldfiles
? I noticed that the latter shows me a listing of files opened in previous Vim sessions and has nothing related to current Vim session. So does "open buffers" mean history of files in current Vim session?
I have read https://vimhelp.org/windows.txt.html and wasn't able to come to any conclusions regarding this.
Upvotes: 1
Views: 2256
Reputation: 196781
Here are additional points…
A buffer is Vim's exact equivalent of a "document" in more familiar programs.
When you create a new document in Word, the program allocates some space in memory and, among other things, gives it a temporary name like Document1
. At that point, you have a "document" named Document1
but there is no file on the disk.
That is exactly what happens when you do $ vim non_existing_file
, with the only difference that what you get is called "buffer", and not "document". You get a "buffer" named non_existing_file
, but there is no file on the disk.
When you open an existing file in Word, the program does the same as above and also maps the document to a file on your disk. At that point, you have two entities, with the document acting as proxy for the file.
That is exactly what happens when you do :e existing_file
in Vim.
When you save a document in Word, the program essentially tries to reconcile the state of the document and the state of the file. If there is no file associated with the document, then you are prompted to create one, and, if there is, then the file is written transparently.
It's basically the same in Vim when there is document/file relationship: :w existing_file
overwrites the file existing_file
with the content of the buffer existing_file
.
The behaviour is slightly different when there is no file: Vim will write a file with the name of the buffer if it has one or abort and complain if the buffer is unnamed.
All that to say that buffers are not that weird a concept as they may seem.
What exactly are other "open buffers" if I have exactly one window open at a time?
Just like you can work with several "documents" at the same time in Word, you can work with several "buffers" in Vim. And just like you can access the other documents in Word, you can access the other buffers in Vim.
Every new buffer that was created on your behalf during the current session is added to a global "buffer list" and :ls
is just a way to display those buffers, so that you can issue commands against them for navigation or management.
Note that the list is global so :ls
will display the same list whether you are in one window or another or in one tab page or another. See this other answer of mine on the relationship between buffers, windows, and tab pages.
As for why you see more items in the list than expected, there are many explanations but it would be easier to explain with a minimal reproducible case.
How is
:ls
different from:browse oldfiles
?
The former lists buffers created during the current session. The latter lists buffers created before the last time the ~/.viminfo
file was written which can be generalized to buffers created during previous sessions.
Upvotes: 5
Reputation: 15186
"Open buffer" is not a valid term in Vim speak, AFAIK. The command :ls
shows listed buffers (and :ls!
shows both listed and unlisted ones). See :h 'buflisted'
option. In short, this is a completely independent attribute. It only serves to "filter out" some buffers.
The help may also use terms "hidden", "visible" and "loaded". "loaded" indicates that a buffer has some text content attached. "visible" - it is shown in one or more windows. Note that all "visible" buffers are "loaded" but not vice versa.
"hidden" is rather informal and usually implies that a buffer is "loaded", but not "visible" (i.e. has been shown in zero windows). Also to note that there could be many buffers that neither "loaded" nor "visible". This is a sort of "editing history" for the current session. They could float in ls
output but they don't occupy much RAM or such.
oldfiles
are not buffers. This is a "history" remained from previous runs. It can be used to re-open some files quickly. Then a valid buffer will be created.
Upvotes: 4