Tips to Work Efficiently With Buffers in Vim
One of the things that determines your productivity in your text editor or IDE is the number of files you’re working with simultaneously. Ideally, you would have to be working with one single file at a time in order to focus your attention and limit the context switching which results in a costly loss of time and focus. But in reality, this isn’t likely to happen, since we’re not working within a single concern at a time (are you?). This implies having to go back and forth between the files, and Vim provides the possibility to handle this switching in a smart way, using buffers.
What is a buffer?
A buffer in Vim is simply a file that is loaded in memory so it can get accessed quickly. This is done automatically as soon as you open it up (typically using the :e command). You can also add a buffer associated to a file without having to edit it directly using
:badd filename command.
Interact with buffers
To displays the open buffers, use the command:
You’ll get a list with the buffers as well as their corresponding data. The first column is the unique number identifying the buffer. The second is the set of attributes. In the above example, there is:
- a buffer with the attribute #, which corresponds to the ‘alternate’ (last edited buffer). You can open it directly using
- a buffer with the attribute %a, which means that it’s displayed in the current window (%), and visible (a). This is technically possible for a buffer to be visible, but not in the current window when working with splits for example.
For more information regarding the
:buffers command, consult the official documentation.
You can close one or multiple buffers using:
:bdelete buffer1 buffer2 ...
To switch between buffers you can use:
To open the next buffer in the list
To open the previous one.
will open up all the available buffers.
Another useful command is
:bufdo which enables you to execute an operation on all open buffers. For example, to do a search replace through all buffers, you can execute:
:bufdo %s/search_tearm/replace_term/g | update
The update keyword at the end automatically saves the buffer if a replace actually takes place. To avoid having to do it every time I’m operating through multiple buffers, I have put:
in my .vimrc which takes care of saving the changes when vim automatically switch buffers.
To learn more about commands related to buffers manipulation, read the Vim FAQ on the subject.
Useful tips to work with buffers
Now, I’ll give you a few tips to work efficiently and move quickly around buffers.
buffers line with vim-airline
vim-airline is considered as a must-have plugin for Vim, as it adds up the graphics features cruelly missing from the default version. To me, the most useful feature is the bar displaying the list of open buffers. This is really useful because it makes you notice when you’re tending to have a list getting too big (which becomes a clear brake to your productivity).
To install vim-airline using Vundle, just place the following line in your .vimrc:
vim-airline uses the Powerline patched fonts in order to display graphics elements within the terminal. The easiest way is to use this GitHub repository.
Clone it using:
git clone https://github.com/powerline/fonts
Then switch to the created repository and run the install script:
The next step is to configure your terminal to use one of the installed fonts (ending with for Powerline). Just be aware that all the fonts won’t necessarily work well with your airline configuration and you might notice graphic bugs. But there will be a few of them that’ll do the job properly though. The one I use is called Liberation Mono for Powerline (I’m using Ubuntu).
I won’t go into the features of vim-airline because it’s not the topic of this article but I’ll share the (basic) configuration I’ve set for it within my .vimrc:
let g:airline#extensions#tabline#enabled = 1 let g:airline_powerline_fonts = 1 let g:airline_theme='powerlineish' let g:airline#extensions#syntastic#enabled = 1 set laststatus=2
The first parameter is what I’m talking about when I say the line displaying the list of open buffers. The second is here to make airline work with the Powerline patched fonts which is mandatory to have the graphics displayed properly. Then, we set the theme, the Syntastic integration and finally we use the status bar everywhere (displayed only when you open a split otherwise).
Now, start Vim and open up a bunch of files sequentially. You’ll have the associated buffers displayed in the top bar, and have the currently open one highlighted:
Swap and close buffers quickly
Here are mappings that I use quite often:
:nnoremap <Tab> :bnext<CR> :nnoremap <S-Tab> :bprevious<CR> :nnoremap <C-X> :bdelete<CR>
They allow you to switch buffers: Tab for the next one, Shift-Tab for the previous one. To quickly close a buffer, I use Ctrl-X. The cool thing is that you can actually see what happens with the vim-airline buffer line:
Fuzzy Search within Buffers with ctrlp.vim
ctrlp.vim is a Vim plugin providing fuzzy finding features for files, MRU (Most Recently Used files), and Buffers. Install it with Vundle:
To open the list of buffers and start searching through it, use:
I’ve decided to map this command to Leader-B:
nmap <Leader>b :CtrlPBuffer<CR>
Now press Leader-B and start fuzzy navigating through the open buffers:
The alternate buffer is always the one selected by default, so to come back to it, just press Leader-B + Enter and there you are.
That’s it ! for any trouble / advice, please go ahead and write a comment.
You can checkout my dotfiles here.Vim and tagged Productivity, Vim. Bookmark the permalink.