Working Code
Imagine you need to edit index.html and style.css at the same time. Vim lets you split the screen and see both.
:e index.html ← open index.html
:vs style.css ← split vertically and open style.css
Your screen splits like this:
┌──────────────┬──────────────┐
│ index.html │ style.css │
│ │ │
│ <div> │ .container { │
│ <h1> │ margin: 0; │
│ </h1> │ } │
│ </div> │ │
└──────────────┴──────────────┘
Move between left and right with Ctrl+w h (left) and Ctrl+w l (right).
Try It Yourself
Open several files from your terminal:
vim index.html style.css app.js
Once Vim opens, run these in order:
:ls ← list the open buffers
:bn ← next buffer (style.css)
:bn ← next buffer (app.js)
:bp ← previous buffer (back to style.css)
:b index ← find buffer by name (partial match works)
Now try splitting the window:
:sp ← horizontal split (same file)
:vs ← vertical split (same file)
Ctrl+w j ← move to the lower window
Ctrl+w k ← move to the upper window
Ctrl+w = ← make every window equal size
:close ← close the current window (buffer survives)
Try tabs as well:
:tabnew app.js ← open app.js in a new tab
gt ← next tab
gT ← previous tab
:tabclose ← close the current tab
"Why?"
Think of it like papers on a desk.
A buffer is a document spread open on the desk. Open 10 files and you have 10 buffers. Some are out of sight but still in memory. They stay open until you clear them with :bd (buffer delete).
A window is a viewport that displays a buffer. You can show the same buffer in two windows at once. Closing a window (:close) doesn't delete the buffer — it just hides it.
A tab is a workspace. Each tab has its own window layout. You can make a "HTML/CSS tab" and a "JavaScript tab" and switch between them.
Remember the key relationship: it's not buffer ⊃ window ⊃ tab. A tab is a window layout, a window is a view onto a buffer, and a buffer is file content. These three are independent concepts.
Deep Dive
Buffer command reference
| Command | Action |
|---|---|
:e {file} | Open a file into a buffer |
:ls | List open buffers |
:bn / :bp | Next / previous buffer |
:b {name} | Switch to buffer by name (partial match) |
:bd | Delete the current buffer |
:b# | Switch to the previously active buffer |
Window navigation shortcuts
Press Ctrl+w followed by a direction key.
| Key | Action |
|---|---|
Ctrl+w h | Window to the left |
Ctrl+w j | Window below |
Ctrl+w k | Window above |
Ctrl+w l | Window to the right |
Ctrl+w w | Next window (cycles) |
Ctrl+w = | Equalize window sizes |
Ctrl+w _ | Maximize current window height |
Ctrl+w | | Maximize current window width |
Run through this scenario yourself:
- Launch
vim(no file). - Open your config with
:e ~/.vimrc. - Split vertically with
:vs. - In the right window, open
:e ~/.bashrc. - Confirm both buffers are open with
:ls. - Jump between panes with
Ctrl+w handCtrl+w l. - Close the current window with
:close, then check:ls— the buffer is still alive.
Key takeaway: closing a window does not remove the buffer.
question: What is a buffer in Vim? answers:
question: Which command splits the screen vertically? answers:
question: Does closing a window delete that file's buffer? answers: