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.