Editing multiple documents in vim: Using vim buffers
When editing a file in vim it is stored and displayed in a buffer, something like a tab in a web browser window. You can open multiple files at once and easily move back and forth among the buffers to work with each file.
Opening multiple files in vim
To open multiple files in vim when you start your session, simply list them on the command line:
$ vim myfile1 myfile2 myfile3 myfile4
This will start vim with four buffers pre-loaded with your four files. You can use wildcards:
$ vim documents/myfile*
If you already have vim running and want to open another file, go to command mode and type:
You’ll be happy to know that you can use tab completion with the
:e command. If you want to create a new file within vim, from command mode run
:enew which will open an empty buffer. Just remember to save the file with a name anytime before you exit:
Navigating among files
To see the buffers open in your current session, from command mode run
:buffers. Note that each buffer lists the file that it contains, and each has a unique number. To move to a different buffer, run
:b# where the # sign represents the buffer number you want to move to. For example, to begin editing the file in buffer number 3, run:
Keep in mind that before you can switch to a new buffer you must save or discard all the changes made to the current one. Use the
:w command to save your file before changing buffers.
You can cut, copy and paste among buffers just as you would within a single file. Just remember that if you make a cut you will need to save the changes in the current file before switching to the target buffer.
When you no longer want to work on a file you can close its buffer with the “buffer delete” command,
:bd. This command closes the file in the current buffer and deletes the buffer; the file is not removed. By adding a number you can delete a specific buffer. For example:
This will close the file in buffer 3 and delete the buffer. And quitting vim will close all files and delete all buffers.
Be seeing you.