Skip to content

Instantly share code, notes, and snippets.

@jbranchaud
Last active January 25, 2024 21:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbranchaud/bccead47f0e75e145db8 to your computer and use it in GitHub Desktop.
Save jbranchaud/bccead47f0e75e145db8 to your computer and use it in GitHub Desktop.
Working With Buffers

Working With Buffers

Me

  • I'm Josh Branchaud
  • I work at Hashrocket
  • Twitter: @jbrancha
  • Github: @jbranchaud

Why talk about buffers?

Using buffers as extensively as we do at Hashrocket is sorta new to me. I used to be more of a vim tab person. These are the buffer-related commands that mostly comprise my day to day workflow at Hashrocket.

Getting Started

Let's just start by editing a file

vim README.md

Listing all our buffers

  • :ls
  • :buffers
  • :files

We'll be using :ls a bunch

Open another buffer

  • :e <filename>
  • :e %:h - from netrw of directory of current file
  • With a plugin, e.g. rails-vim

A Note about netrw

  • vim .
  • i to toggle display modes - credit to Brian Dunn

Again, why talk about buffers?

The key to working efficiently in Vim with buffers is being able to move between buffers quickly.

When you can move quickly between buffers you can work more quickly and (hopefully) better avoid getting distracted and losing your train of thought.

Open existing buffers

  • :help :b
  • :b [N]
  • :b <tab completion>

Previous Buffer

  • c-^ (<ctrl><shift>6)

Back and Forth

  • ctrl-o - back a buffer (ish)
  • ctrl-i - forward a buffer (ish)

How does vim know where to go with ctrl-o and ctrl-i?

Jump List

  • :help jump-motions
  • :jump or :jumps

Sometimes I have a certain jump location in mind, but after moving around different files, it gets buried.

Marks

  • :help mark-motions
  • m[a-z] - intra-file marks
  • m[A-Z] - inter-file marks

Unloading Buffers

  • :help :bd
  • :bd - to unload current buffer
  • :bd [N]
  • :bd <filename>
  • :n,mbd
  • :%bd - unload all the buffers?

Wipeout Buffers

  • :help :bw
  • :bw - to wipeout the current buffer
  • etc.

So what's the difference between :bd and :bw?

The :bd command really just removes the file from your buffer list and not much else. The :bw command on the other hand wipes out everything about that buffer. It's entries in the jump list go away, it's marks go away, etc.

Ask the Audience

  • What sorts of cool stuff do you like to do with buffers that I didn't mention?
@Trid-collab
Copy link

Thank you for this explanation. I am a newbie trying to learn vim. This helped to clear the confusion I had about buffers:
Could you please explain what is the difference when we open a file using :e filename and :tabe or :tabnew filename. I ask this because I find the navigating behavior different in both the case. When I use :e - I cannot use gt or gT to navigate whereas when I use :tabe I can

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment