Drill 7: More On Buffers


Review of Last Week

Last week's Drill was a whirlwind overview of Windows, Tabs and Buffers. I demoed my own workflow, where I quickly navigate between test, implementation and reference windows and tabs.

This Week's Drill

This week we're going to focus more on buffers, and switching between them. The big emphasis is on our workflows rather than individual keys and commands. Next week we'll go back to some specific keystrokes to practice, but for this drill, I want to emphasise how important your workflow is, and how by consciously designing it, you can train your muscle memory to work quickly, and efficiently. The key is to narrow down the gigantic world of commands Vim offers you, and focus on the subset of commands that complement how you work.


This is only a smattering of commands related to buffers. Please see help for more. Browsing help is an excellent way to get a feel for what is worth training yourself on.

:buffer <filename><tab> - Start typing the filename of an open buffer, and hit tab to complete it. If you like this behavior, be sure to checkout the CtrlP plugin I recommend below, as it has this feature, but on steroids.

:bn and :bp - Next and previous buffer.

:bdelete 1 - Delete buffer number 1. Cleaning up your buffer list as you finish up with files lets you use the other commands more fluidly, without a bunch of junk between you and what you want to see.

<C-^> - Switch to "alternate file". Three fingers need to splay across the keyboard for this command (control, shift, 6). But regardless of that contortion, This command has become very common in my workflow. It allows me to gf (go file) without worrying about how to get back.


nnoremap <leader>q :bp<bar>sp<bar>bn<bar>bd<CR> - I found this mapping recently on a StackOverflow thread. It closes a buffer without closing the window it belongs to.

Expanded out, it runs four commands in order. Buffer Previous, Split, Buffer Next, and finally Buffer Delete. The delete closes the split window that is created, and you're left with the same window you started with, but with a closed buffer.


I haven't recommended any plugins in VimDrills yet, but I think that buffer switching is an excellent time to introduce the CtrlP plugin. I won't go into detail on how to install plugins (keep an eye out, I'll do a video soon), but I will share with you a mapping I have for CtrlP:

nnoremap <silent><leader>b :CtrlPBuffer<CR> - Then start typing a bit of a filename of an open buffer, and hit enter to switch to it.


This week is going to be another workflow focused week. I can't tell you the exact workflow that will be best for you, but I ask that you're concious of the layout of your workspace. Heck, even draw out your workflow on paper.

Things to think about this week:

  • How do you open a file?
  • How do you open the test for that file?
  • How do you lay out your window to maximize the amount of code you see?
  • How do you find reference files?
  • Are there side benefits to having narrower vertical splits? (having under 80 or a 100 columns of text shown may push you to write more succinct code)
  • Could mappings help you optimize any of these steps?

And throw me a comment down below, I love hearing from you.


comments powered by Disqus