This course covers how to open up multiple files or multiple buffers, and navigate between them. You'll also learn how to execute commands over every buffer with a ":bufdo" command as well as how to work with hidden buffers. You get a chance to follow along with an exercise in which you practice working with multiple buffers.
Then we look at windows in Vim which allow you to view more than one of those buffers at the same time or view the same buffer in multiple places, all at once.
This course is ideal for anyone who needs to edit text files in a command-line environment using Vim.
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
The goal of this exercise is to give you practice working with multiple buffers. First start a command line session on your local machine. And next use VIM to open all the files that start with the characters b-u-f that came in the course downloads. And to do that, navigate to the location of these files. And remember, this could be different for you depending on where you extracted the contents of the course archive. This example assumes that the course download was saved into your downloads folder and extracted from there. So we'll go to downloads vimclass. Now we'll open all the files that start with b-u-f. First, let's look at the buffer list with a buffers command. You could also use the LS or files command as well. Now use the buffer command or :b for short to switch buffers. Let's open the buffer associated with a buf-bed.txt file by using its unique number. And to do that, we can type :b2 and press enter. Let's use the B command again, but this time let's use a file name. Let's switch to the buf-cat.txt file. So we can do :b buf-cat.txt and press enter. Now let's use tab completion to open the buffer associated with the buf dad command. So he can use :b space press tab. And we'll have to hit tab a couple more times until buf-dad.txt is highlighted, and now press enter. Let's quickly return to the previously opened buffer by using control carrot symbol. Now let's look at the buffers with L-S. Buffer three displays percent A, which means it's the buffer in the current window and that it's active. Buffer four displays, the pound sign, which makes it the alternate buffer. Let's switch back to buffer four, with control carrot symbol. Let's move to buffer three with BP and press enter. Let's keep going back to buffer two by typing BP and enter again. Now let's reverse the direction and get back to buffer three with a BN in command. Let's practice getting to the first buffer with :bF and enter. We can quickly get to the last buffer with :bl. Let's make a change to this current buffer. I'm just gonna insert some text here and now let's try to switch to buffer two, by typing :b2. If you don't have the hidden option enabled which you probably don't by default, you'll get an error message like you see on the screen here. You can also follow the instructions here by doing :b!2 and press enter. Now buffer two is loaded into the window, and let's look at our buffers list with :ls and press enter. You'll notice that the previous buffer has the indicators H and plus which means it's a hidden buffer with modifications. Now let's enable the hidden option by typing :set hidden and pressing enter. Now let's make a change to this buffer. For example, I can just insert some text here and type in bed, hit escape to return normal mode. Now let's switch to buffer one with :b1 and press enter. You'll notice that you didn't get an error message. Now let's look at our buffers with :ls. You'll now notice buffers two and four have the H and plus indicators, which means that they are both hidden and they both have modifications. Let's open up the file named nav.txt for editing. One way to do that is type :e nav.txt and press enter. Now we're looking at the contents of this nav.txt file. And let's take a look at our buffers lists with :ls. Let's say you decide you don't wanna edit the nav.txt file. So delete it by typing colon and BD and pressing enter. Now, let's look at the buffers with LS and that buffer number five, nav.txt is gone. Let's delete another buffer. Let's do :bd3 and press enter. Make sure it's gone with LS and sure enough buffer three has been deleted. Now let's use the explorer to open the help.txt file. And we can start the explorer by using colon capital E and pressing enter. Now you can use any VIM navigation commands you know to place your cursor under the help.txt file. And I'm just gonna use a forward search here. I'll do forward search h-e-l-p, press enter. And now my cursor is under helped.txt. And when I hit enter, like now that file is loaded into a buffer and displayed in my window. When you're done with this file you can delete this buffer with :bd and pressing enter. Let's perform a global substitution in all the buffers and let's substitute the pound sign with the at sign. So to do that, we can use bufdo, the global range, substitute command, the pound sign, the at sign, and g flag. And let's hit enter to execute this command. Now let's look at the buffers with LS. You can see that they're all modified because each file has a plus indicator. Let's go to the first buffer with :bf and make sure that the substitution happened there. And sure enough, it did. If you wanna abandon all your changes so you can do this exercise again in the future, you can use :qall, which stands for quit all. We'll need the exclamation mark to force this quit because there are some buffers here that have changes made to them.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.