Working with multiple files
So far in the course, we’ve used Vim to edit just one file. This is fine for learning the basics of how Vim works, but in real life this is quite limiting. Most text editing tasks require us to work with multiple files at once — programmers and software engineers, for example, usually end up working across multiple source files in the same editing session.
Vim has support for editing multiple files in the same session with buffers and windows. In this lesson, we’re going to focus on buffers. These are a prerequisite to understanding what windows are and how they work.
In Vim, a buffer can be thought of as the in-memory text of a file — in essence, a file loaded into Vim for editing. You can make changes to the buffer, and the original file on disk will remain untouched until you decide to write. Strictly speaking, buffers don’t need to have a corresponding file on disk, but most of the time they do.
Vim lets us have multiple buffers open at the same time, which corresponds to us being able to modify multiple files in the same editing session.
In the editor, the file that’s currently open is called demo. However, there is another file called myfile. Let’s try opening it — to do this, type :e myfile. This will open myfile in a new buffer.
Once you’ve opened myfile, try switching between the buffers with :bn and :bp, short for buffer next and buffer previous.