Vim has never been like most editors. I've used different editors (Notepad++, Atom, VSCode, and yes, emacs, briefly). Most editors use tabs and windows system. Vim has buffers, windows, and tabs. Some of these abstractions Vim use are different.
If you are new to Vim, I will explain what buffers, windows, and tabs are. I will also show how to use them efficiently.
Table of Contents:
- Vim buffers
- Vim windows
- Vim tabs
- Thinking in 3D
- Using buffers, windows, and tabs efficiently
Before we start, if you haven't, add
set hidden in your
~/.vimrc and source it. Without this option, whenever a changed buffer goes to "hidden", Vim will prompt you to save the file. For more information about this setting, check out this article and
A buffer is the in-memory text of a file.
When you open a file in Vim, it creates a new buffer. Do this from your terminal:
After running this, Vim creates one buffer for
file1.js. This buffer is stored in-memory.
Exit Vim. Back to console, run:
vim file1.js file2.js
Vim creates 2 buffers: one for
file1.js and one for
file2.js. You are probably still seeing
However, right now Vim has 2 buffers. You can see all buffers with
There are several ways to go between
file1.js buffer and
:bnextto go to next buffer (
:bprevto go back)
:buffer, then type the name. Vim can autocomplete with
:bufferNwhere N is buffer number.
:buffer2for example, will jump to buffer #2.
- Jump between your last 'position' with
<Ctrl-i>. This is not buffer specific, but it works.
- Toggle between previous file with
For method #2, you can combine buffers list with buffer name. For example, with this mapping, pressing
<Leader>b will display my buffers and types
:buffer<space> command. All that is left for me to do is typing the buffer number or buffer name (that Vim can autocomplete).
:nnoremap <Leader>b :buffers<CR>:buffer<Space>
Once a buffer is opened, it remains in your buffers list. We have two buffers opened:
file1.js buffers. Still in the same vim session, if we want to open a new file, we can do
:e file3.js. If we check with
:ls, we'll see that we have 3 buffers.
You can type
:bdelete to close a buffer. To be honest, in my years of editing with Vim, I almost never needed to delete buffers.
The way I see it, buffer is like Z axis in X-Y-Z coordinate. Imagine X axis to right, Y axis to top, and Z axis towards screen. Your buffers files are lined up in the Z axis. You can traverse the Z axis one file at a time with
:bprev. You can jump to any coordinate in Z axis with
:buffer <filename>. The number of file buffers you have is how long your Z axis is. Vim makes it almost frictionless to travel anywhere along this Z axis.
A window is a viewport on a buffer. In Vim, you can have multiple windows opened.
From your console, run this again:
What you are looking at the screen is one window that displays buffer
file1.js. Window is what you are seeing buffers through.
Exit Vim. In console, run
vim file1.js file2.js
We've seen this many times now. This time, we have two buffers and still one window.
Don't quit vim yet. Run:
You are looking at two windows. It's kind of hard to see the texts on bottom window, but the bottom one says
file1.js (and top one
file2.js). The top window is a viewport for buffer
file2.js, the bottom window is a viewport for buffer
Remain in this vim session - don't quit yet. Run:
You are now seeing three windows. Top left window displays
file3.js buffer. Top right window displays
file2.js buffer. Bottom window displays
I hope you are starting to see the difference between vim buffers and windows. Don't worry if you aren't - take your time. It took me a while to understand it when I was learning this.
You can have multiple windows displaying one buffer. Right now I am still on top left window that views
file3.js buffer. I will type:
:ls :buffer 2
Now both top left and top right windows are displaying
file2.js buffer (bottom window still displays
file1.js buffer). If I start typing on top left, you'll see that both top left and top right window are changing as I type.
To close current window, you can run
<Ctrl-W>+C. Alternatively, you can do
:quit. When you close a window, a buffer will still be opened (check
Some useful shortcuts for window:
<Ctrl-W>+v # Opens a new vertical split <Ctrl-W>+s # Opens a new horizontal split <Ctrl-W>+c # Closes a window <Ctrl-W>+o # Makes current window the only one on screen and closes other windows <Ctrl-W>+h/j/k/l # Moves the cursor to left/bottom/top/right
Some useful Ex commands:
:vsplit <filename> # Split window vertically :split <filename> # Split window horiontally :new [filename] # Create new window
For more, check out
A tab page is a collection of windows. In Vim, a tab has different meaning than most text editors. In most text editors (and modern browsers), a tab usually means an open file/ page. When we close it, that file/page goes away.
In Vim, a tab does not represent an open file. We just learned that vim saves opened files in buffers. When we close a tab in vim, the files in that tab are still stored in buffers. A tab can have one or many windows. Think of tabs like layouts or templates.
Let's try it. In console:
file2.js in new tab:
More tabs navigation:
:tabnew file.txt # open file.txt in a new tab :tabclose # Close current tab :tabnext # Go to next tab :tabprevious # Go to previous tab :tablast # Go to last tab :tabfirst # Go to first tab
You can also run
gt to go to next tab page in Normal mode.
To start vim with multiple tabs, you can do this from console:
vim -p file1.js file2.js file3.js
Moving between windows in Vim is like traveling along X-Y axis in cartesian coordinate. We are moving two-dimensionally. We can move to top, right, bottom, left with
Earlier I said that Vim buffers are like Z axis. Inside each window, you can move along the Z axis with buffer navigation. When we combine window movement with buffer traversal, we are moving in three-dimensional space.
Each window can view any of our buffers.
This X-Y-Z movement is possible thanks to Vim's window and buffer features.
To learn how to use buffers, windows, and tabs efficiently is to understand what they are designed to do.
Use buffers to open up all required files to get current task done. It may be eight or eighty buffer files. Having many buffers opened doesn't effect spatial distribution. Vim has designed switching file buffers to be frictionless. Use that to fly between buffers.
Use windows when you need to view multiple buffers. Like when diffing files, referencing codes, or following code flows.
Use tabs when working on different projects. Like one tab for server codes and one tab for client codes.
In most code editors there are 2 abstractions in their workflows: windows (ex: split screen) and tabs. In Vim there are 3: buffers, windows, and tabs. When I started using Vim full-time, it required a significant paradigm shift. My personal suggestion is to take your time to use it the way they were designed first. If after you've tried it and you still don't think it's for you, use the process that suits you most.
Ultimately, do what works for you. Just because a certain feature was designed to for X doesn't mean everyone should do X. Feel free to experiment around and find your best workflow.
This is a good place to stop. Thank you for reading. Happy coding!