Essential and practical vi – Part 3


What started out as a plan to make a not too complex post about vi buffers, turned out to be a 3 part post with lots of essential bits and pieces on this awesome editor. So also see part 1 and part 2.

There is even more advanced things I could write about like the .vimrc file and its contents and the many plugins that are out there. But, in my opinion these series contains the most practical knowledge to use vi efficient on a daily basis.

A while back I was looking at an efficient way to edit more files at once in vi. Looking around you’d find out that there are natively globally two ways to do so:

  1. With tabs
  2. With buffers

I picked up on the first fast because it looks and feels familiar (as in every browser you use) and practical because switching between tabs is easy. But then I stumbled upon this post and this explanation on Stackoverflow.

The TLDR: if you truly want to edit multiple files at once as you would in any other text editor, with all functionality the same in all files, you should use buffers and not tabs. I can recommend reading the links if you would like to know more. This is why this post is about buffers.

It seems there are many, many ways you can open up files simultaneously, edit them, save them, close them, switch between them, etc. etc. The below might not even work for you, but for me this is the most practical way to work with buffers and resembles the way I’ve used TextEdit years before for quick notes and such.

Open up multiple files at once:

$ vi myfile.txt otherfile.txt justanother.txt

Three files are open, but you see only one. To see them all, let’s view all files in their own window:


This opens up the files in the default horizontal split. My terminal is almost always full screen and I like the vertical mode more:

:vert ball

Now you don’t have to show all these files at once with the above commands… even when you see just one, the files are open and you can see them with:


And switch between them with:


Which is for buffer next and buffer previous. To switch to a specific buffer number, which you’ve discovered with :ls, you can use:


Which jumps to buffer number 4. You can also use the filename directly and tab completion:

:b openfileinbuffer.txt

When you need full screen and many files open at once, the above will work. With only a couple of files, I would like to see my files I’m working on:

:vert ball

With all buffers open, switching between them is easy:


OK, three files open. Let’s says we want to open a completely new one, in a new buffer and window. By default this can be done with:


But since I like vertical split more, I’m using:


Give your new file a name immediately:

:vnew newfilefromvim.txt

Or edit an existing file:

:e ~/existingfile.txt

What the above does though and is a bit annoying, is it opens the file in the same window. The other file is still there, in its own buffer, but you have to open it again to see it. Therefore I like:

:vs ~/existingfile.txt

Better, as it edits the existing file and opens it in its own, vertical window.

There is also the build in vi file explorer, which you can open with a simple:


This does, again, use the current window though which I find, again, a bit annoying. A:


Solves this. Your view can get cluttered fast though, so I myself don’t like it that much. Close the file explorer with:


All these files are in buffer, remember. Now we want to close one of these files. Jump to it with CTRL WW and quit it with :q or close the windows with :close

Have a look at your buffers like before with :ls. As you can see, the quitted file or closed window is still in your buffer. When you are working with buffers and really want to close one, user buffer wipe:


You might not care, but I like a clean workspace.

So i’m guessing we’re having about 4 files open at this point? These might be files beloning to a particular project that needs working on tomorrow as well. You can easily save your current session with:

:mksession! ~/

Save and quit all files with:


And tomorrow start where you left off with:

$ vi -S ~/

Awesome and easy! The a in the :xa command stands for ‘all buffers’. So you can also exit all files with:


That’s it for vi for now!

Update: Complete series here, here and here! Next up: installing screen.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.