Buffers and Windows in Vim

Felipe Juárez
5 min readDec 5, 2017

--

Introduction to vim buffers
Introduction to vim buffers

A few weeks ago, I wrote a post about handling buffers, just the basics. Today, we’re going further, because I’ll show you how to work with windows and buffers.

Before going deeper, let’s take a step back and recap what we know about buffers.

Buffers

So, what we know about buffers? Well, here we learned the following:

  1. A buffer is a file loaded into memory for editing.
  2. All opened files are associated with a buffer.
  3. There are buffers not associated with any file.
  4. Buffers are identified using a name and a number.
  5. The name of the buffer is the name of the file associated with that buffer.
  6. The buffer number is a unique sequential number assigned by Vim.
  7. That number will not change in a single Vim session.
  8. You can have as many buffers as you want.
  9. Also we have learned the following commands:

This is nice, but those are a lot of commands and it seems hard to work through a lot of buffers. Yeah! that’s right, but we can do a couple of things to facilitate our work and our life.

The first thing that we are going to learn is:

Windows

Before going deeper, I’m going to ask you to forget everything you know about windows. We’ll start once you have done it.

That was quick! 😆. Well, let’s start.

The reason for this request is because in Vim the concept of a window is a little different that you have been working until now. A window in Vim is a graphical representation of a buffer, i.e. the way that we see the content of a buffer is in a window. In matter of fact, when you start Vim with ❯ vim, you are seeing an empty buffer loaded into a window. The way you manage your windows is up to you:

  • You can use multiple windows on one buffer
Multiple windows, one buffer
  • Or several windows on different buffers.
Multiple windows, multiple buffers

That’s nice, but how can I manage them? That’s a good question but before running we need to walk. So, first we need to know how can we create a new window.

New windows

As I said before, we need to know how to create new windows but, believe me or not, we already know how to do it. Let me help you, this is how we generate a new window:

With those commands you’re always starting a new window with an empty buffer. But, what happen if I want to see the content of a file? Well, You have two options for doing it:

  • Using the :edit file command or
  • Passing the name of the file to the :new command or to the :vnew command.

If, for so special case scenario, you need to see different parts of the same file, Vim has a shortcut for doing it.

Buffers and windows are independent. That means you can navigate through one buffer in one window while the other buffer in the other window stays where you left.

Tip: We have aliases for those commands: CTRL-W s is the same as :sp and CTRL-W v is the same as :vs

But, careful you must be my young padawan, buffers and windows are independent only for navigation, if you make changes in one buffer, both windows are modified.

That’s nice, but every time that I opened a new Window the pointer moves to it. How can I move back to my previous window? Short answer, that behaviour only happens when you are in a non empty buffer. But, in the next section, we will learn how to move through windows.

Moving through windows

It won’t happen too much time before you see your Vim tiled with several Windows. So, moving between them will be a required skill that you will need before that happens. Fortunately, those skills aren’t hard to learn. Hands on!.

First we are going to divide our Vim in three windows as follows:

Three windows layout

If you pay close attention you will see the cursor in one of the windows (upper left corner), let’s call it window number 1, the one below will be window number 2 and the right one will be window number 3.

If you are having troubles creating the layout. First create the right window with :vnew and then the below one with :new.

If you followed the instructions above, the cursor will be in the window number one. So, to move one window below just hit CTRL-w j, to move back to the window number 1 hit CTRL-w k and to move to the right one just hit CTRL-w l. I hope you have noticed it, but in case you have missed it: h, j, k and l keys, serve as well to move through text and through windows the main difference lies in the combination CTRL-w before typing the movement keys.

But is there any way to rearrange my windows? Of course!.

Rotating windows

In Vim we are owners of our windows and buffers and we can do whatever we want, actually we can enable options only for a file type or for a buffers. But that’s another history. Now we are going to stick to the question, How can I rearrange my windows? For that, we have the following options:

And what about resizing windows?

Window resizing

Of course you can do it!

And how about closing a window?

Closing windows

Well, that’s easy. Once you have done editing the buffer in that window. It’s time to close it, and for that we have the following options:

Conclusions

Through this post, we learned how to handling windows in Vim. And also we recap our knowledge about buffers. Even though it’s a short post, I hope it can help you on your way to reveal the amazing features a tool like Vim has.

But for this post we are done. I hope you find it interesting and helpful. and as usual, If you have any comments or any doubts please let me know.

See you next time. Good luck and have fun!

--

--

Felipe Juárez

Software Developer at MakingDevs, a competitive gamer currently playing SC2 and Clash Royale. I love beer, anime, manga, music my kids and my wife