Unleash your inner root

Vim: Mastering your workspace: buffers

In previous articles I’ve thrown out a few simple tips like basic movement, and multi-line edits. Which are both some very basic happy things to make your life in vim better. Today I’d like to tackle things from a different direction. I want to talk to you about truly MASTERING your work space. To do that we’re going to talk about windows, buffers, and tabs (oh my).

First: A little perspective.
I am a firm believer in the “command line is king” way of life. I believe that to truly master Linux, you should immerse yourself in the terminal, and do everything possible there, without x-forwarding if possible.

That being said, if you live completely in the terminal workspace begins to become an issue. The idea of having multiple editors open in different windows to glance at reference documentation CAN be an issue. In my early linux/vim day’s I would frequently save and exit one file, just to open another to glance at something, and then close that new file and open the original document again: and I’m sure I wasn’t alone in that.

The good news is: That DOESN’T have to be your life! There is a solution. That solution comes layered in three magical parts. If you guessed “windows, Buffers, and tabs” you guessed correctly! Give yourself a gold star.

So, lets talk buffers.

Buffers are what you work with all the time! Anytime you open a file in vim, you are opening a buffer. The buffer is held in memory, and when you issue a “:w” to save the file, the buffer gets saved back into the file. Easy to understand right? YUP! dead simple. What many newbies don’t realize is that vim can have multiple buffers open! Yup, You can have several buffers open at once, and its incredebly useful!

If you are walking into an editing situation you open multiple buffers right off the bat with:

vim <file1> <file2> <file3> <file4>

“Wait a second Brandon” You may be saying, “i just did that, and only the first file is opened! You LIED to me?! I’m so hurt.” Now now, don’t be like that. I didn’t lie, I just haven’t told you the next part yet! Vim can have multiple buffers open at once, but only one of them is displayed in a window(note the use of window as a word, we’ll come back to this soon). There are three vim commands you need to know to to handle this new found power of yours:

:ls
:bnext (shorthand :bn)
:bprevious (shorthand :bp)

Lets break it down.

:ls lists what buffers you have open, for example if I’m in a vim window, and I have “test.sh” and “example.txt” opened and i issue “:ls”, I will see the following:

test.sh                                                                                                                                         1,1            Top
:ls
  1 %a   "test.sh"                   line 1
  2 #h   "example.txt"               line 1
Press ENTER or type command to continue

The top line signifies the name of the buffer you are currently editing, as does the %a next to the number. line 1 on each signifies what line on each file I am currently on, if i were to scroll down a few lines, and issue :ls again I might see:

test.sh                                                                                                                                         4,1            Top
:ls
  1 %a   "test.sh"                   line 4
  2 #h   "example.txt"               line 1
Press ENTER or type command to continue

The other two commands :bnext and bprevious (henceforth :bn, and :bp) allow you to switch between your buffers. For example: I’m currently editing buffer 1, “test.sh”, if i issue :bn” my window will change to “example.txt”, and upon issueing :ls again I’d receive:

example.txt                                                                                                                                      1,1            Top
:ls
  1 #h   "test.sh"                   line 4
  2 %a   "example.txt"               line 1
Press ENTER or type command to continue

It’s important to note, that buffers are cyclical, so if you are on buffer 2, and issue :bn, you will be taken back around to buffer 1, and likewise from buffer 1 if you issue :bp, you will be taken around to buffer 2.

And lastly you can also issue :b# to go directly to the buffer you want(handy if you have a large number of buffers open at once. IE:

b2

As a closing note to buffers, you can also issue :bdelete to close out the current buffer completely and remove it from your list of buffers. Inversely if you later think of NEW files you need to work on, you can issue :e to open NEW buffer. Also if you issue a filename that does not exist vim will open an empty buffer that will save to that filename).

This article has gotten just a wee tad bigger than I’d normally like, so it looks like we’ll have to split this into multiple ones. Next time I’ll talk about windows (windows are freaking awesome. Trust me you will LOVE how they just open up the workspace and make it feel MUCH bigger.)

buffersvim

Brandon.Graves • February 18, 2016


Previous Post

Next Post

Leave a Reply

Your email address will not be published / Required fields are marked *

%d bloggers like this: