In the last months I have seen a couple of posts talking about Vim, from Why I Still Use Vim to Vim isn’t that scary. Here are 5 free resources you can use to learn it.
In order to keep that increment, today I’m going to talk about it. In particular we are going to talk about
buffers and how to work with them.
But before going deeper, we need to talk a little about Vim.
Au contraire of the common thinking, Vim is a “text manipulator”, and that’s the main reason that you don’t start in insert mode (input text) as usually, in other editors.
If you are asking Why? It’s for the philosophy of Vim. In Vim, is all about editing text efficiently and usually you will spend more time moving through existing text and editing it than you do typing stuff. And for that, you have six modes (usually you use, only three or four modes) for editing:
- Command line
- Ex mode
Vim released publicly in 1991 after Bram Moolenaar improved Bill Joy’s text editor (Vi) for Unix. Even though, Vim have almost 30 years in the market, it is still one of the most popular and best editors in 2017. Why? There are a couple of reasons for using Vim (Why I Still Use Vim, The Top 7 Reasons To Give The Vim Text Editor A Chance, Why I use Vim) but, for me the short answer is: tmux + zsh + vim is a killer combo.
I’m not going to say that Vim is easy to understand, because is not. But, when you really understand the basics of Vim, hell! it really pays off.
Well, after the brief introduction, let’s start with the topic.
So, what is a buffer. In simple terms, a buffer is a file that has been loaded into memory. But, it is fair to say that, a buffer is the simplest unit in Vim, why? because you are going to use it for almost everything, from navigating the directory structure, to visualize files in tabs or windows (Thery aren’t the ones you know).
In Vim, you can have as many buffers as you want, and you can arrange as you wish. And every opened file is associated to a buffer, but you can have buffers not associated to any file.
But, how I create a new buffer? Well, you have a couple of options.
:newSplit vim in two horizontal windows (that’s why I told you that windows in vim, are not the ones you know)
:vnewYes, you guessed it. This command split vim in two vertical windows.
:edit fileOpen the name of the file and automatically create a new buffer.
The first thing you need to know about buffers is, if you opened a file in Vim and modify it a little bit, you are forced to save it, if you want to open a new buffer/file. That’s because you don’t have enabled the option to edit multiple buffers without saving the modifications.
To enabled this option you should use
:set hidden option.
Vim hidden option
This video shows how to edit multiple buffers without saving the modifications made to a buffer while loading other…
As we can see above, we modify a file removing three lines and then we try to open a new buffer, after that we see an error message
E37: No write since last change (add ! to override) that’s because we are not allowing to change from buffers without saving it, we can remove it using the
:set hidden command. After that we can change from buffers easily, but if we try to exiting vim we are going to see two message errors (E67, E162) specifying that we are not allowing to exit from Vim, until we save or discard changes from the mentioned buffer.
Moving through buffers
Once you have created a couple of buffers, how do I navigate through all of them? Well, you can list all of your oppened buffers with
Reading from left to right the first column indicates the buffer number, second one is the indicators column those chars mean the buffer status:
% the buffer in the current window,
h means a hidden buffer,
# the alternate buffer for
a an active buffer: it is loaded and visible. The third one means the name of buffer and the last one shows the number of the line in which the pointer is.
Once you have displayed all of them, you can move to a particular buffer with
:buffer N or
:b N (
N means the number of the buffer). Or if you want to switch between previous and next buffers you could use
bnext) respectively. Or if you want to move to the first buffer or last buffer, you could use the
:blast) options. You can even open all buffers in a new window with
Once you have finished to modify a file and don’t want to see it in the buffer’s list you can use
:bdelete arg or
:bd arg, Where
arg is the file’s name or the buffer’s number.
If you want to delete multiple buffers you have two options. * Passing a range argument to the
- Passing multiple buffer names (also, you can use buffer numbers) to the
Through this post, we show how to handle vim
buffers and how easy is to work with them. Even though it was a short post, I hope this can help you to going deeper in Vim. It’s worth mentioning that you can view a buffer in a
window or in a
tab (yes, the ones you know) being
windows default option.
For this post we are done, I hope you can find 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!