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.
Intended Audience
This course is ideal for anyone who needs to edit text files in a command-line environment using Vim.
Prerequisites
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
Resources
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
In the lesson on buffers, we talked about reasons you might want to have multiple files open at once for editing. We looked at different ways to open those files and buffers and quickly switch between them. But what we didn't do was cover how to view more than one of those buffers at the same time, or how to view the same buffer in multiple places, all at once. Well, in this lesson on windows, we'll be doing just that. In Vim, a window is a view of a buffer. By default when you start Vim without supplying any file names as arguments, Vim starts with one window open and it loads an unnamed to buffer into that window. If you start Vim by passing in one or more files to it, Vim will start with one window open and it will display the first buffer, which is an in memory representation of the first file in that window. Previously, when you were working with buffers, you were using the same window. For example, when you opened a file for editing with a :e or a :edit command, you loaded that buffer into the current window. When you move to the next buffer in your buffer list with bn or bnext, you loaded that next buffer into your current window. The whole time you were working with a single window. However, when you were using the help system, you were using two windows. These were two windows that were split horizontally on your screen. If you remember way back to that lesson on the help system, you learned how to switch between those windows with a Ctrl+w, Ctrl+w command. What Ctrl+ww really does is it cycles through all of your open windows. If you only have two windows open, this effectively switches between them. You also learned how to close the window with a :q or a :quit command which is how you can close any window regardless of what is being displayed in that window. So enough about what you already know, let's start adding to it. I'm going to change into where I extracted the contents of the course download archive. Now, I'm going to open up all the files that start with the characters buf. So I'll type vim buf* and press enter. At this point, we're looking at one buffer in one window. If you wanna split the window horizontally, type sp and press enter. sp is the short form of the split command, so you can use :sp or :split Now, we're looking at the same buffer in two different windows. So if you ever find yourself needing to view or edit two sections of the same file, this will come in handy. Since you're looking at the same buffer in two different views, any edits made to the buffer will be displayed in both windows. Let me insert some texts at the top of our window here with ant and press escape. Notice how the edits also appear in the lower window even though I'm only active in the top window. Let's close this window with :q and press enter. You can also use the Ctrl+ws command to perform the same action as the split command. So I'm going to hold down the control key, type w, release the control key, and then type s. Now we're looking at the same buffer in two different windows. Let's close this window again with :q and press enter. If you want to open a different buffer or a file in the newly split window, supply that to the split command. If the file is in the buffer list, then that buffer is loaded. If that file is not already in the buffer list, then it's added to the buffer list and displayed in the newly created window. So let's do this with :sp buf-bed.txt and press enter. So now we're looking at two different buffers loaded into two different windows. Let's close this newly created window with a :q in our command. If you wanna view buffers side by side, then use the vsplit command. The short version of that command is a vs So let's vertically split this window with :vs and press enter. Now, we're looking at the same buffer in two windows that are placed side by side. Let's close this window with :q You can also use the Ctrl+wv command to do the same thing. Let me demonstrate that now. So I'm gonna hold down the control key, type w, release the control key and type v. All right, so that does the same thing as the vs command. Let's go ahead and close this with :q and hit enter. Now, to load a different buffer in the newly created vertically split window, supply the file or buffer name to the vsplit command. So let's do this. Let's do :vs buf-cat.txt and press enter. By the way, we've been using the :quit or :q command for short to close the current window. You can also close the window with Ctrl+wq Let me do that now. I'll just hold down the control key, type w, release the control key and type q. Now the window is closed. You can have more than just two windows open at once. Let's create four windows and load our four buffers into them. So we already have one window with one buffer in it. Let's load buf-bed into it with sp buf-bed.txt Moving on, let's open our next buffer into a window with :sp buf-cat.txt And let's open our last window with sp buf-dad.txt Now, we have four windows open at once. If you have one more room on your screen, you can keep going if you'd like. You already know that you can close the current window with Ctrl+wq You could keep repeating that command to close the other windows as well. However, if you want to close all the other windows except for the one you're currently in, use the :only command. This makes the current window the only window on the screen. The short version of the only command is :on and the Ctrl+w shortcut is Ctrl+wo I'm just going to type :on and press enter. Now all the other windows are closed and we're left with this one window only. You can combine horizontal and vertical splits to make a more complex grouping of windows. Let's open up our four buffers in a square pattern. I'll type sp buf-bed.txt And this places our new window on top of our current window. Now, let's vertically split this window with :vs buf-ant.txt Now, we have three windows open. Let's split the bottom window into two vertical windows. First, we need to navigate to that window. And let's do that with the Ctrl+ww command, So I'll do Ctrl+ww and were moved over one window, and Ctrl+ww again, and we're moved to the next window. Okay, let's split this window with :vs for vertical split, buf-cat.txt and press enter. So now we're looking at four different buffers in four different windows. You can make even more complex patterns by performing more splits. Let's split this window with :vs So you get the idea here. Let me go ahead and close out with :q So far you only know how to cycle through the windows with Ctrl+ww Let's go ahead and do that quickly and just cycle through all these windows. Now we're in the bottom left-hand corner, Ctrl+ww, bottom right. Ctrl+ww, to the top left. Ctrl+ww, to the top right and so on. I'm just gonna stop in the top left window there. By the way, you can also use Ctrl+w in conjunction with the HJKL pattern. So if you want to go to the window below the current window use Ctrl+wj To go to the window to the right Ctrl+wl To go to the window above your current window, Ctrl+wk And, of course, to go to the window to the left, you're gonna use Ctrl+wh If you work with windows often, you could create mappings to make this even quicker. For example, you can map Ctrl+h to Ctrl+wh, Ctrl+j to Ctrl+wj and so on. You can also map these using the leader key as well. For more information about mapping, see the lessons on Vim settings in the vimrc file. By the way, if you're using a graphical version of Vim, you can just simply click in a window to make it the active window. I'll just click in this window, and that window, and this window, and it becomes active, and so on. Also, if you wanna change the size of a window, you can just click on one of the window separators and drag it to your desired size. So I can click on this status bar that splits these windows and drag it up and down, however small or big I would like to make these windows. And the same thing with the vertical split, I can drag it to the right and left and so on. Then you can just do all kinds of crazy arrangements here that you would like to do. Vim also provides a few shortcuts you can use to resize windows as well. For example, to increase the height of a window, use Ctrl+w followed by the plus sign. Let me do this again. Ctrl+w+, Ctrl+w+, and so on. To decrease the height of a window, use Ctrl+w followed by minus. To increase the width of a window, use Ctrl+w followed by the greater than symbol, here. And to decrease the width, use Ctrl+w followed by the less than symbol. To maximize the height of a window, use Ctrl+w_ To maximize the width of a window, use Ctrl+w followed by the pipe symbol. To make all the windows the same or equal size, use Ctrl+w= Let's open another window in the top row with :vs.buf-cat.txt Now, let's say you have all your windows arranged the way you like them but you want to rearrange which buffers are displayed into which windows. One way to do this is with a Ctrl+wr shortcut which rotates the windows. Here, I'll type it now, Ctrl+w, lift off the control key and hit r. Now the first window becomes the second window, the second window becomes the third window and the last window becomes the first window. Notice how this rotates everything to the right. If you were dealing with just horizontally split windows, then Ctrl+wr rotates everything down. Let's do this again. Ctrl+wr, Ctrl+wr You can also use Ctrl+wR This performs the rotation in the opposite direction. So let's do that now. Ctrl+w+Shift+r. The first window becomes the last window. The second window becomes the first window, and so on. In addition to just rotating windows, you can be more explicit using Ctrl+w followed by capital H, capital J, capital K, or capital L. Let's start with Ctrl+wJ. This moves the current window to the very bottom using the full width of the screen. Likewise Ctrl+wK will move the current window to the very top of the screen using the full width of the screen. Ctrl+wL moves the current window to the far right, using the full height of the screen. Let's do that now with Ctrl+w+Shift+l Ctrl+wH moves the current window to the far left using the full height of the screen. And we can do that now with Ctrl+w+Shift+h By the way, you can still use all the buffer commands you know in any window. For example, let's load the first buffer into this window with :b1 and pressing enter. Now we can go to the next buffer with bn, the last buffer with :bl, so on and so forth. Speaking of buffer commands, let's list our buffer windows with :ls You'll see that every buffer has an a indicator, meaning that they're all being actively displayed in a window. The buffer that's in the current window has the percent sign indicator. Let's add another file to our buffer list with :badd modes.txt and press enter. Now, let's look at our buffer lists, :ls The buffer we just added is not being displayed in a window, so it doesn't have the a indicator. So if you were wondering when you might ever see an a indicator without the percent sign indicator, well, now you know. It's when you're working with multiple windows. Let's close all the other windows except the current window with :only and press enter. If you want to open all the buffers in your buffer list in their own window, use the ball command or the shorter version of that command, :ba, and press enter. If you don't have enough room on your screen to open all the buffers in your buffer list, Vim will open as many as possible and tell you that it couldn't fit them all on your screen. Let's get back to just two open windows. I'll close all the other windows with :on and press enter. Now let's open another buffer in a new window with :sp buf-bed.txt In the buffers lesson you learned about the bufdo command, which allows you to execute commands on all of your open buffers. There is a very similar command called windo, which you can use to execute a command in every window. Let's substitute the pound sign with the at symbol in all of our open windows with :windo %s/#/@/g and press enter. So you can see that our substitution worked. Let's bring one of our other buffers into a new window with :sp buf-cat.txt Notice that it still contains pound signs. Again, the windo command operates on windows, while the bufdo command operates on buffers, whether or not they're being displayed in a window. Before we wrap up this lesson, I wanna go back to the window commands we've been using. Let's pull up the help on the Ctrl+ww command with :h ^ww and press enter. There are two ways to execute this command. One way is Ctrl+w, Ctrl+w, meaning that you hold down the control key, type w, continue holding down the control key and type w again. The other way is Ctrl+ww, meaning you hold down the control, key type w, release the control key and then type another w. This holds true for pretty much all the window commands you've learned. To get a list of those commands, you can use :h ctrl-w and press enter. Try both versions of the commands and see which one works for you. Also be aware that there might be a couple of gotchas when you use the control versions of some of these commands. For example, Ctrl+s suspends screen updating on some terminals. So if you type Ctrl+w, Ctrl+s to split a window and nothing happens, what you need to do is to type Ctrl+q to resume screen updating. Likewise Ctrl+w, Ctrl+q, might not work because some terminals intercept that Ctrl+q combination. Well, that does it for this lesson on windows. If you wanna read up on all the various details, be sure to check out the help documentation at :h windows
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.