Buffers, Windows, and Tabs

How Vim juggles many files at once: buffers, windows and tab pages, the argument list, splits, :bufdo and :argdo, and opening files from the shell.

By The Dark DebuggerUpdated 20 min read

Real work touches more than one file: a service's config, the nginx site in front of it, the log that shows what went wrong. This article explains Vim's model for that (buffers, windows and tab pages), the argument list, splits, commands that run across many files, and how to open the right set of files straight from the shell. You should be comfortable with the material up to Visual Mode and the :s command from Search and Replace.

Why this matters

A common pattern for people new to Vim: open app.ini, read a value, :q, open nginx.conf, :q, open the log, :q, open app.ini again. Every round trip throws away the undo history, the cursor position, registers and search state, and costs a few seconds of re-orienting.

Vim is built to keep many files open in one session. The catch is that its vocabulary doesn't match other editors. A "tab" in Vim is not a file, closing a window does not close the file, and "buffer" is the word you'll use most. Once the model clicks, switching between files becomes faster than reaching for a mouse.

The examples use a small project with this layout:

shop/
├── conf/
│   ├── app.ini
│   ├── nginx.conf
│   ├── redis.conf
│   └── worker.ini
└── logs/
    └── app.log

The model: buffers, windows, tab pages

TermWhat it isRough analogy
bufferthe in-memory text of a file (or of nothing yet)a document you have open
windowa viewport onto one buffera pane on the screen
tab pagea collection of windows, a layouta virtual desktop

Every file you open becomes a buffer and stays in the buffer list until you remove it. A window shows exactly one buffer, but a buffer can be shown in several windows or in none. A tab page holds one or more windows.

Here is one tab page with three windows, which I built with :vsplit conf/nginx.conf and :split logs/app.log after opening conf/app.ini, then :windo set nowrap so long lines don't wrap (screen captured from a 100x18 terminal):

2026-11-09T10:42:17Z INFO  checkout started on :80|[server]
2026-11-09T10:42:19Z WARN  cache miss rate 41% (re|port = 8080
2026-11-09T10:43:02Z ERROR payment provider timeou|log_level = debug
~                                                 |
~                                                 |[cache]
~                                                 |host = redis.internal
~                                                 |port = 6379
~                                                 |~
logs/app.log                    1,1            All|~
upstream checkout {                               |~
    server 127.0.0.1:8080;                        |~
}                                                 |~
server {                                          |~
    listen 443 ssl;                               |~
    server_name shop.example.com;                 |~
    location / { proxy_pass http://checkout; }    |~
conf/nginx.conf                 1,1            Top conf/app.ini                   1,1            All
:windo set nowrap

Each window has its own status line with the file name. The :tabs command describes the same layout in words:

:tabs
Tab page 1
>   logs/app.log
    conf/nginx.conf
    conf/app.ini

> marks the current window.

Buffers

Opening and listing

:e path (:edit) opens a file in the current window, creating a new buffer if it isn't already open. Tab completes paths. :ls (also :buffers or :files) lists the buffers. I started Vim with three files and ran :ls straight away:

vim conf/nginx.conf conf/app.ini conf/redis.conf
:ls
  1 %a   "conf/nginx.conf"              line 1
  2      "conf/app.ini"                 line 0
  3      "conf/redis.conf"              line 0

The number is the buffer number, which never changes for the life of the session. The flags between the number and the name tell you the buffer's state:

FlagMeaning
%the buffer in the current window
#the alternate buffer, where <C-^> takes you
aactive: loaded and shown in a window
hhidden: loaded, but not shown in any window
+modified, not yet written
uunlisted (only visible with :ls!)

line 0 means Vim hasn't loaded the file yet; it reads files lazily the first time you visit them.

Switching

CommandGoes to
:b 3 or :3bbuffer number 3
:b redthe buffer whose name contains red (Tab completes)
:bn / :bpnext / previous buffer in the list
:bf / :blfirst / last buffer
<C-^>the alternate buffer (#): toggles between the last two

:b with part of a name is the fastest of these once you have more than a handful of files. The part must match exactly one buffer, though. All three buffers here have conf in their name:

E93: More than one match for conf

:b ngi was unique, and :ls afterwards showed nginx as current and app.ini as alternate:

:ls
  1 %a + "conf/nginx.conf"              line 1
  2 #h   "conf/app.ini"                 line 1
  3      "conf/redis.conf"              line 0

<C-^> (Ctrl and the 6 key on most keyboards) then flips between those two. It is the single most useful buffer key: source file and its test, config and the log it produces.

Removing buffers: :bd and :bw

:bd (:bdelete) unloads a buffer and removes it from the list; :bw (:bwipeout) also forgets its marks and options, so it really is gone. After :bd 3, the buffer still exists as unlisted (u) and :ls! shows it:

:ls!
  1 %a   "conf/nginx.conf"              line 1
  2  h   "conf/app.ini"                 line 1
  3u     "conf/redis.conf"              line 1
  4 #h   "conf/worker.ini"              line 1

After :bw 3 it is no longer listed at all, not even by :ls!. In daily use :bd is what you want. A trap to know about: :bd also closes every window that shows the buffer. When I showed app.ini in all three windows of a split layout and ran :bd 1, Vim was left with a single window.

The hidden option

Without any configuration, Vim refuses to switch the current window away from a buffer with unsaved changes. I deleted a character in nginx.conf and ran :bnext:

E37: No write since last change (add ! to override)

Vim is protecting you from losing work, because by default a buffer that leaves the screen is unloaded. You can write first (:w), discard with :bnext! (dangerous), or tell Vim that buffers may stay loaded in the background:

:set hidden

After that, :bnext worked and :ls showed the edited buffer as hidden and modified:

:ls
  1 #h + "conf/nginx.conf"              line 1
  2 %a   "conf/app.ini"                 line 1
  3      "conf/redis.conf"              line 0

The work isn't lost or saved; it's waiting. Vim won't let you quit with modified hidden buffers either. :qa told me which one:

E37: No write since last change
E162: No write since last change for buffer "conf/nginx.conf"

:wa writes all modified buffers, :qa! discards everything, and :xa writes and quits. I recommend set hidden in every vimrc (see Configuring Vim); without it, multi-file work fights you at every step. set confirm is a good companion: instead of an error, Vim asks whether to save.

The argument list

The files you name on the command line form the argument list ("arglist"), a second, separate list on top of the buffer list. It is the set of files you said you want to work on, and it doesn't grow when you :e other files. :args shows it, with the current one in brackets:

vim conf/*.ini
[conf/app.ini]  conf/worker.ini
CommandWhat it does
:argsshow the list
:next / :prevedit the next / previous file in the list
:first / :lastjump to either end
:args conf/*.inireplace the list (globs work, including **)
:argadd fileadd a file to the list
:argdo cmdrun cmd in every file of the list

The arglist's real value is :argdo. Both config files had debug logging switched on:

grep -rn 'log_level' conf/
conf/worker.ini:3:log_level = debug
conf/app.ini:3:log_level = debug

One command changes and saves both:

:argdo %s/log_level = debug/log_level = info/ge | update
"conf/app.ini" 7L, 81B written
"conf/worker.ini" 4L, 57B written
grep -rn 'log_level' conf/
conf/worker.ini:3:log_level = info
conf/app.ini:3:log_level = info

Two details make this safe to run on many files. The e flag stops :s from raising an error in a file with no match (an error would abort :argdo), and update writes only files that actually changed, unlike :w.

Windows

Splitting

CommandResult
:split file / :sp filenew window above, showing file (current file if omitted)
:vsplit file / :vs filenew window to the left
:new / :vnewsplit with an empty buffer
:sb 3split and show buffer 3
<C-w>s / <C-w>vsplit / vsplit the current window

With set splitbelow splitright, new windows open below and to the right instead, which most people find more natural.

Moving around and rearranging

Every window command starts with <C-w> (Ctrl-W), followed by one more key:

KeysAction
<C-w>h j k lmove to the window left / below / above / right
<C-w>w / <C-w>pnext window / previous (last used) window
<C-w>H J K Lmove the current window to the far left / bottom / top / right
<C-w>x / <C-w>rexchange with the next window / rotate windows
<C-w>Tmove the current window into a new tab page
<C-w>c or :closeclose the current window
<C-w>o or :onlyclose every other window
<C-w>q or :qquit the window (Vim exits if it was the last one)

Resizing

Keys or commandAction
<C-w>=make all windows (almost) equal
<C-w>+ / <C-w>-one line taller / shorter (10<C-w>+ for ten)
<C-w>> / <C-w><one column wider / narrower
<C-w>_ / <C-w>|maximise height / width
:resize 5set the height to 5 lines
:vertical resize 30set the width to 30 columns

On the three-window layout above, :resize 5 in the log window and :vertical resize 30 in the nginx window produced this (a narrower 80x22 terminal, before I turned off wrapping):

2026-11-09T10:42:17Z INFO  che|[server]
ckout started on :8080        |port = 8080
2026-11-09T10:42:19Z WARN  cac|log_level = debug
he miss rate 41% (redis.intern|
al)                           |[cache]
logs/app.log   1,1         Top|host = redis.internal
upstream checkout {           |port = 6379
    server 127.0.0.1:8080;    |~
}                             |~
server {                      |~
    listen 443 ssl;           |~
    server_name shop.example.c|~
om;                           |~
    location / { proxy_pass ht|~
tp://checkout; }              |~
}                             |~
~                             |~
~                             |~
~                             |~
~                             |~
<nf/nginx.conf 1,1         All conf/app.ini                   1,1            All
:vertical resize 30

Notice the :resize 5 window holds 5 screen lines, not 5 file lines, because of wrapping.

Closing a window doesn't close the file

From the three-window layout, I ran :q in the app.ini window and then :ls:

:ls
  1      "conf/app.ini"                 line 0
  2 #a   "conf/nginx.conf"              line 0
  3 %a   "logs/app.log"                 line 1

app.ini is still in the list; it's simply not displayed (no a flag). Without hidden it was also unloaded, which is why it shows line 0; :b 1 brings it straight back. Windows are views. Use :bd when you want the file out of the session.

Tab pages, used correctly

CommandAction
:tabedit file / :tabe fileopen file in a new tab page
:tab splitthe current buffer in a new tab page
gt / gT / 3gtnext / previous / third tab page
:tabclose / :tabonlyclose this tab / all the others
:tabmove 0move this tab to the front
:tabslist tab pages and their windows

I moved the app.ini window into its own tab with <C-w>T, then opened redis.conf with :tabedit conf/redis.conf:

:tabs
Tab page 1
    logs/app.log
    conf/nginx.conf
Tab page 2
    conf/app.ini
Tab page 3
>   conf/redis.conf

The way to think about tab pages: one tab per task, not one tab per file. A tab with the service code and its test side by side, another with the config and the log, a third for a git diff. Inside each tab you switch buffers with :b, <C-^> and friends.

Treating tabs as browser-style file tabs works against Vim. :e and :b change the buffer in the current window, so the tab "for" one file soon shows another, and vim -p stops creating tabs after ten files (see Common mistakes).

Running a command everywhere: :bufdo, :windo, :tabdo

CommandRuns in
:argdo cmdevery file in the argument list
:bufdo cmdevery listed buffer
:windo cmdevery window in the current tab page
:tabdo cmdevery tab page

:windo is the one for display settings. The :windo set nowrap in the first screen capture applied nowrap to all three windows at once, and :windo diffthis (used in Real-World Vim Workflows) turns on diff mode in every visible window.

:bufdo and :argdo move the current window through each buffer, so the hidden rule from earlier applies. Without hidden, a substitution that modifies the first buffer stops the loop. With app.ini and worker.ini open and no vimrc:

:bufdo %s/= debug/= warn/ge
E37: No write since last change (add ! to override)

The loop stopped after changing the first buffer. Either end the command with | update (as in the :argdo example), or set hidden and run :wa after reviewing the changes.

Opening many files from the shell

The shell is often the best place to choose which files to edit:

CommandOpens
vim a b cthree buffers (and arglist entries), showing the first
vim -o a bstacked horizontal windows
vim -O a bside-by-side vertical windows
vim -p a bone tab page per file
vim -d a bdiff mode (see Real-World Vim Workflows)

vim -O conf/app.ini conf/worker.ini on an 80x10 terminal:

[server]                                |[worker]
port = 8080                             |queue = orders
log_level = debug                       |log_level = debug
                                        |concurrency = 4
[cache]                                 |~
host = redis.internal                   |~
port = 6379                             |~
~                                       |~
conf/app.ini          1,1            All conf/worker.ini      1,1            All
"conf/worker.ini" 4L, 58B

Combining Vim with grep -l opens exactly the files that mention something:

grep -l 'redis.internal' conf/* logs/*
conf/app.ini
logs/app.log
vim $(grep -l 'redis.internal' conf/* logs/*)
:args
[conf/app.ini] logs/app.log

Command substitution splits on whitespace, so it breaks on file names with spaces. I copied app.ini to conf/app (old).ini and ran the same kind of command; Vim received the name in pieces:

[conf/app]   (old).ini    conf/app.ini

Vim opened two empty buffers called conf/app and (old).ini. The robust form uses NUL-separated names, and xargs -o (GNU xargs) to reconnect Vim's input to the terminal so it works interactively:

grep -lZ 'redis.internal' conf/* | xargs -0 -o vim
[conf/app (old).ini] conf/app.ini

Where you'll use this

  • Rolling a change across a service's config: a hostname, a port or a feature flag that appears in several files. vim $(grep -rl …) plus :argdo … | update, as shown below.
  • Reading code next to its tests or docs: a vertical split, <C-w>w to hop, <C-^> in either window to bring back the previous file.
  • Incident work: one tab page with the config you're changing and a split showing the log, another tab with the runbook.
  • Reviewing a batch of generated files: open them all as the arglist and walk through with :next and :prev.

A complete example. The Redis server behind the shop is moving to redis-02.internal. First find every file that mentions the old host:

grep -rn 'redis\.internal' conf logs
conf/sessions.ini:3:url = redis://redis.internal:6379/2
conf/app.ini:6:host = redis.internal
logs/app.log:2:2026-11-09T10:42:19Z WARN  cache miss rate 41% (redis.internal)

The log must not be edited, so search only conf/, open the matching files, and change them in one pass:

vim $(grep -rl 'redis\.internal' conf)
:args
:argdo %s/redis\.internal/redis-02.internal/ge | update
[conf/sessions.ini] conf/app.ini
"conf/sessions.ini" 4L, 77B written
"conf/app.ini" 7L, 85B written

The escaped dot in redis\.internal matters: an unescaped . matches any character. Check the result from the shell before reloading anything:

grep -rn 'redis' conf
conf/sessions.ini:2:backend = redis
conf/sessions.ini:3:url = redis://redis-02.internal:6379/2
conf/app.ini:6:host = redis-02.internal

Common mistakes

E173 when quitting

vim conf/nginx.conf conf/app.ini conf/redis.conf

I visited app.ini with :bnext, then typed :q:

E173: 2 more files to edit

Vim tracks your position in the argument list, and :bnext moves through the buffer list, so as far as the arglist is concerned you never left the first file. Vim warns because you opened three files on purpose. If you're done, :qa quits without complaint (when nothing is modified); typing :q a second time also works.

E93: More than one match

:b conf fails when several buffer names contain conf. Type more of the name, press Tab to see the candidates, or use the buffer number from :ls.

vim -p silently stops at ten tabs

I opened twelve files with vim -p many/*.conf. The tab line ended at site10.conf, and asking Vim for the number of tab pages and listed buffers printed:

10 12

Files 11 and 12 were loaded as buffers but got no tab, with no warning. The limit is the tabpagemax option:

  tabpagemax=10

You can raise it, but this is Vim hinting that tabs are not meant to be one per file. Open many files as buffers and switch with :b.

Closing the window and expecting the file to be gone

:q or <C-w>c closes a window. The buffer stays in :ls (and in memory with hidden), and :bnext will cycle back to it. Use :bd to drop it from the session.

Summary

  • A buffer is a file's text in memory, a window is a view of one buffer, a tab page is a layout of windows. Closing a window never closes the buffer.
  • :ls shows buffers with flags (% current, # alternate, a active, h hidden, + modified); switch with :b N, :b part-of-name, :bn/:bp and <C-^>.
  • set hidden lets you switch away from modified buffers; without it you get E37. Finish with :wa, :xa or :qa!.
  • The argument list is the set of files you opened on purpose; :args, :next, :prev, and :argdo %s/…/…/ge | update to change them all safely.
  • Splits: :sp, :vs, then <C-w> plus hjkl to move, = to equalise, o to keep only the current window, T to move it to a new tab page.
  • Use tab pages for separate tasks, not separate files; vim -p stops at tabpagemax (10).
  • From the shell: vim -O a b for side by side, vim $(grep -l pat files) for "everything that mentions X", and grep -lZ … | xargs -0 -o vim when names can contain spaces.

Exercises

Create the shop/ project from the start of the article (any content works, as long as app.ini and worker.ini contain a log_level = info line).

Exercise 1: Jump by nameeasy

Start Vim with conf/app.ini conf/nginx.conf conf/redis.conf. Without using buffer numbers, switch to redis.conf in as few keystrokes as possible, then use :ls to find out which buffer <C-^> would take you to.

Show Solution
:b red
:ls
:ls
  1 #    "conf/app.ini"                 line 1
  2      "conf/nginx.conf"              line 0
  3 %a   "conf/redis.conf"              line 1

red matches only redis.conf. The # flag is on app.ini, so <C-^> returns there. nginx.conf was never visited, so it still shows line 0.

Exercise 2: Quit cleanlyeasy

Start vim conf/app.ini conf/nginx.conf, then type :q without doing anything else. What does Vim say, why, and what is the clean way out?

Show Solution
E173: 1 more file to edit

You opened two files and are still on the first entry of the argument list. :qa quits immediately; Vim's exit status was 0 when I checked with echo $?. If you meant to look at the second file first, :next gets you there.

Exercise 3: Change every config at oncemedium

Set log_level = warn in every .ini file in conf/, writing only the files that change, with a single Ex command after opening them. Make sure a file without log_level doesn't stop the command.

Show Solution
vim conf/*.ini
:argdo %s/^log_level = info$/log_level = warn/e | update
"conf/app.ini" 7L, 84B written
"conf/sessions.ini" 4 lines, 77 bytes
"conf/worker.ini" 4L, 57B written

sessions.ini has no log_level line: the e flag kept the loop going, and update didn't write it (that's why it shows the "lines, bytes" message instead of "written"). grep -rn log_level conf confirmed both other files now say warn.

Exercise 4: Build a review layoutmedium

Starting from vim conf/app.ini, build this layout: app.ini on the left taking half the width; on the right, the log on top and nginx.conf below it. Turn off wrapping in all three windows with one command.

Hint

:botright vsplit makes a full-height window on the far right, whatever the current layout.

Show Solution
:botright vsplit conf/nginx.conf
:split logs/app.log
:windo set nowrap
[server]                                         |2026-11-09T10:42:17Z INFO  checkout started on :80
port = 8080                                      |2026-11-09T10:42:19Z WARN  cache miss rate 41% (re
log_level = warn                                 |2026-11-09T10:43:02Z ERROR payment provider timeou
                                                 |~
[cache]                                          |~
host = redis-02.internal                         |~
port = 6379                                      |~
~                                                |~
~                                                |~
~                                                |logs/app.log                    1,1            All
~                                                |upstream checkout {
~                                                |    server 127.0.0.1:8080;
~                                                |}
~                                                |server {
~                                                |    listen 443 ssl;
~                                                |    server_name shop.example.com;
~                                                |    location / { proxy_pass http://checkout; }
~                                                |}
conf/app.ini                   1,1            All conf/nginx.conf                 1,1            All
:windo set nowrap

The :split happens in the window you're in (nginx, on the right), so the log appears above nginx on that side. With a plain :vsplit the new window would open on the left instead.

Exercise 5: Survive spaces in file nameshard

Create a copy named conf/app (old).ini. From the shell, open every file under conf/ that contains log_level, in a way that handles the space, and change warn to error in all of them in one command.

Show Solution
grep -rlZ 'log_level' conf | xargs -0 -o vim
:args
:argdo %s/^log_level = warn$/log_level = error/e | update
[conf/worker.ini]  conf/app (old).ini conf/app.ini
"conf/worker.ini" 4L, 58B written
"conf/app (old).ini" 7L, 85B written
"conf/app.ini" 7L, 85B written

grep -Z ends each name with a NUL byte, xargs -0 splits only on NUL, and -o gives Vim the terminal as its input, so it behaves normally instead of warning that input is not from a terminal.


Tested with: Vim 9.1 (Ubuntu 24.04 package 9.1.0016) on Ubuntu 24.04 (WSL2), November 2026, started as vim -u DEFAULTS (no personal vimrc). Every command ran in a real terminal session inside tmux, and the screens and messages above are captures of that terminal; the 12-file vim -p check counted tabs with tabpagenr('$'). Nothing here was run on Windows or macOS.

Was this page helpful?
The Dark Debugger

Written by The Dark Debugger

Engineer documenting the things that took me too long to learn, so they take you less time. Every Vim tutorial here is tested end to end before publishing.