Working with vim buffers

This week I decided to focus on familiarizing myself with buffers. Buffers are what makes working with multiple files possible in Vim. Up until now I have only worked with a single file at a time. Obviously you can only do this for so long before you start going mad. In order to share what I've learned, I thought I could compare how some of these trivial actions would compare from Sublime Text to Vim.

Before we start, I'm going to create some random files for us to work with so you can follow along if you want. Let's first open up the command line and navigate to the Desktop.

cd ~/Desktop

Now we will create a temporary folder to work in and navigate into it. I'll name mine vimbuffers.

mkdir vimbuffers && cd vimbuffers

Now lets create three txt files.

touch file-{1,2,3}.txt

Let's add a single line in them so we're not working with empty files.

echo "This is file 1" > file-1.txt

Repeat this for the other two files. We can view what we added to the file by typing the following.

cat file-1.txt

Now that we have these three files set up, let's take a look at what we will cover.

Opening a single file with the editor

Sublime Text Vim

In Sublime Text
This is some real 101 type stuff, but let's look at this anyway. To open up a file in Sublime I usually right click on the file and select open with Sublime Text. Pretty basic.

In Vim
In order to do the same thing with vim you would type vim file-1.txt, assuming you are in the vimbuffers directory.

Opening a single file once in the editor

Sublime Text Vim

In Sublime Text
If I wanted to open a file in Sublime with the editor already open, I would use command + o or you could use the file > open menu option.

In Vim
To open a file with vim already open, you type :e file-1.txt in normal mode (Not sure if you're in normal mode? Just hit the ESC key). :e stands for edit.

Opening multiple files

Sublime Text Vim

In Sublime Text
When I want to open multiple files in Sublime I tend to do the same thing as above and use the command + o but select multiple files or a directory.

In Vim
With Vim I can either type vim file-1.txt file-2.txt file-3.txt or use a wild card and open all .txt files in the current directory with vim *.txt

Seeing what file you have open

Sublime Text Vim

In Sublime Text
To see all the files we have open in Sublime, I can use the side bar. I can toggle the side bar with command k + command b or find it in the View menu.

In Vim
Now that we have files loaded in vim, you can view them with :ls. This command is easy to remember if you use the command line often because to list the contents of a directory you use the ls unix command.

Switching files

Sublime Text Vim

In Sublime Text

The most basic way to switch files in Sublime is to simply select it in the side bar. There are many more efficient ways to do this but for the sake of the article we will stick with that.

In Vim
To select or load a file you can use the :b command which is short for :buffer. To load the file you want from the buffer list you would type :b followed by a file name or buffer number like this: :b1.

Random bonus tip! To load the file you had previously open you can use :b #.

Closing a file

Sublime Text Vim

In Sublime Text

This is where we start seeing a bit of a difference, as far as I know you cant "unload" a file from Sublime nor have I ever attempted to.

In Vim
To remove or delete a file from the a buffer you would use :bd which is short for :bdelete and then the filename or number. You would do this if you had a file in the buffer list you were no longer working on.

Conclusion

This is a very simple intro on how to work to with multiple files in vim. If you want to know more, you can watch the screen cast by Derek Wyatt Working with Many Files (Screencast 1) and also this video in vimcasts, Working with buffers.