Mastering Neovim Buffers

A buffer is an in-memory representation of text that can be edited. A buffer can be empty, or it can contain the content of a file.

When a buffer contains the content of a file, it contains a copy of that content; edits made in the buffer do not change the original file, unless the buffer contents are saved back to the original file.

Buffers can exist in several states, where the most common are:

active
A buffer that is displayed in a window. If this buffer is associated with a file, its content has been read into the buffer, and the buffer is given the name of that file. If the buffer has been modified since the file was read its content may different from that of the file.
hidden
A buffer that is not currently displayed. Hidden buffers are the same as active buffers, they are just currently visible.

Buffers are held in a "buffer list", where each buffer is assigned a unique number that persists throughout a session and can be used to identify and navigate between buffers.

Note that Neovim will not allow you to exit if there is a buffer that is both hidden and modified, which can cause confusion with new users and has led to various memes. This is done to protect you from losing edits, if you try to exit when there are unsaved edits Neovim will raise a warning and activate that buffer so that that you can choose to save that content or throw it away with the :quit! command.

In the next section we will see how to create new buffers.