# Vim Buffers

You can think of a buffer as a piece of text loaded into memory. They’re especially useful when you need to move text between files using Vim’s built-in copy / paste functionality. You can yank and put between any buffers in the system, but you can’t do it between buffers in other windows.

For example. I have two windows open in MacVim (this could be two separate terminal windows too). I can yank and put text between any buffer or tab within one of the windows but I can’t yank text from one window and put it in another. For me the practical result of this is to have one window open per project, because I’m not usually copying text between projects. Also, keeping only one project per window makes opening files easier.

Note that those pieces of “text loaded into memory” aren’t just files you’ve opened. They can be new “buffers” of text that you’re using as a temporary scratch-pad or new files you just haven’t saved yet. There’s also a concept of “registers” which are similar. You typically use registers to store bits of text you’ve yanked.

## Listing all your open buffers

There are two ways to do this. The easiest is to use the bufexplorer plugin. This plugin displays a list of all the open files, and makes it easy to find the one you want and switch to it.

You launch the “Buffer Explorer” by typing <Leader>be Once you get there move your cursor onto buffer you want to bring back into view, and hit <ENTER>. When I’m looking at this I usually have lots of files present so I just seach for the name of the one I want, hit <ENTER> to finish the search, and <ENTER> again to load that file’s buffer.

__Be sure to hit <F1> in Buffer Explorer to see all the great options for displaying your buffers.

### Doing it without plugins

Vim, of course, doesn’t require plugins, and maybe you’ve SSH’d onto a system that doesn’t have bufexplorer. This happens to me all the time. In that case you’d type :buffers (or :ls or :files) to see a listing of them at the bottom of your screen. If your window is short it’ll just fill the viewport.

You’ll notice you can’t search or select text in this area, and there’s no obvious way to choose one of those buffers to switch to. The key to using these buffers is to see what number is next to the file you want.

In the example image there hello_world.md is number 7. I have a couple options for switching to number seven. Before I leave that view I can type :buffer7 (or :b7). I can also do that outside of the view (after hitting <ESC> or q).

When you’re not in that view you can also type 7 followed by <CTRL>^. This seems nonsensical, but I suspect that it’s based upon the idea that typing <CTRL>^ with no number will always jump you to the most recently used buffer. So if you keep hitting it, you’ll toggle back and forth between the same two buffers. I think that adding the number option is intended to build on that muscle memory. I.e. <CTRL>^ is what you’re expected to be hitting regularly and thinking of as “switch buffer” and sometimes you say “oh, do that thing I do all the time, but to buffer 7 (or whatever).”

There are more options like :bnext and :bprevious which you can read about in :help buffer-list.

## Buffers & Changes

When you’re editing files in Vim, you’re really editing buffers. When you switch between buffers, or close a tab, Vim “unloads” them by default. In general Vim will complain when you try and close an unsaved buffer.

When you’re working on the terminal you don’t have a lot of visual space and you may wantto switch back and forth between buffers without saving. You can add this to your ~/.vimrc to tell vim to not “unload” buffers that aren’t currently visible.

set hidden


Once hidden is on Vim won’t loose your unsaved changes because the buffer remains loaded in the background.

Having hidden on can sometimes be confusing to new users sometimes because all your open tabs have been saved, but when you try and close the window Vim complains that you have unsaved changes from that file you mucked with but never saved. You thought you’d just abandoned those changes, but no, Vim’s still got them for you. Switch to that buffer and :quit! or :write or undo. Note: If you’ve got other tabs or split windows open quit will just close the current one.

Ok. Great. A list of the files you have open. If you’re like me, and pretty much every Atom or Sublime user out there, each of those files has a tab across the top of your window. So, why should you care?

## Reasons to Care about file buffers

### You’ve lost the tab.

My coworkers tend to have approximately 423 tabs open at any given moment. I don’t understand how they find anything.

In Vim you can load the buffer explorer, then search for, or scroll to, the file you’ve lost, then hit enter and it will switch you to that tab. Yay. Tab found.

### You’ve accidentally closed the tab… without saving!

Think of the Window (not tab) as a house. Moving something around in one room, and then leaving the room doesn’t mean your changes disappear into the ether. Nope, they’re still there, even if you haven’t saved them. Switch back to the buffer that was loaded into that tab and your unsaved changes will still be there waiting for you.

Also see undo

### You’re not in a graphical client.

Imagined you’ve ssh’d onto a server, you want to yank some text from one file and put it into another one1. Open one file, then in the same Vim session open another file. You can then use the buffer explorer to switch between them, yanking and putting as you go.

### You don’t like tabs

Same thing as when you’re in a terminal client. It’s a great way to switch between “open” files while you work without the clutter of multiple windows or tabs.

## Opening a buffer in a new tab

When you open the buffer explorer there’s a number to the left of each buffer. Once you know the number you can load it in a new tab with :tabnew | b <number> For example, I just ran: :tabnew | b 55 to load my plugins post. Yes, I have a lot of posts open right now. ;)

## Deleting a buffer

It’s rare you actually care enough to delete one. But, maybe you’re running really low on memory and loaded something huge. Maybe you just like tidying up. Maybe you ran :Explore (see opening files).

1. Yank? Put? What happened to copy paste? Check out the Cut, Copy, & Paste post