Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 36 ยท Buffers, Windows and Tabs

Tab Pages and Layouts

Learn Vim tab pages: create, switch, move and close them, when tabs beat buffers and splits, per-tab directories with :tcd, and how to label the tab line.

If you come from VS Code or a browser, "tab" means "one file". In Vim it does not. A tab page holds a whole layout of windows. You might keep a tab for "the backend: model, controller and test in three splits" and another for "the notes and the TODO list". Switching tabs switches the entire arrangement at once.

Because of this, most Vim users work mainly with buffers and use tab pages for a few big contexts. Trying to use one tab per file leads to a confusing mix of the two models.

Creating and closing tabs

Command Effect
:tabnew New tab with an empty buffer
:tabedit file or :tabe file New tab editing file
:tab split Open the current buffer in a new tab (a quick "zoom")
:tabclose or :tabc Close the current tab and its windows
:tabonly or :tabo Close all other tabs
vim -p a b c Start with each file in its own tab

Ctrl-w T moves the current window out into a new tab. :tab split is useful because it gives you a full-screen copy of the window; when you are done, :tabc goes back to your layout untouched.

Moving between tabs

  • gt goes to the next tab; gT to the previous one.
  • 3gt goes to tab number 3. :tabn 3 does the same.
  • :tabfirst, :tablast and g<Tab> (back to the last-used tab).
  • :tabs lists tabs with their windows.
TEXT
Tab page 1
    main.py
>   utils.py
Tab page 2
  + notes.txt

Here > marks the current window and + marks a modified buffer.

Reordering

:tabmove 0 moves the current tab to the first position. :tabmove alone moves it to the end. :tabmove +1 and :tabmove -1 shift it right or left. Positions are counted from zero.

Tabs, buffers and windows compared

When to use which

Buffers Many files, one view
Splits Two files at once
Tabs Separate contexts

Start with buffers, add splits when you compare, tabs when you switch tasks.

1 / 2

A directory per tab

Each tab can have its own working directory with :tcd path (tab-local cd). :lcd is window-local and :cd is global. Setting :tcd ~/project-a in one tab and :tcd ~/project-b in another means relative paths and :find behave differently in each context. :pwd shows the directory that applies to the current window.

Running a command in every tab

:tabdo %s/foo/bar/ge applies a command in the current window of each tab. Pair it with :tabdo update to save. Compare it with :bufdo, which iterates over buffers; if a file is hidden but not in a tab, :tabdo will not touch it.

The tab line

When there is more than one tab, Vim draws a tab line on top. The default shows the file name, a + for modified buffers and a count of windows when there is more than one. You can control it with the showtabline option (0 never, 1 when two or more tabs, 2 always). A custom label is built with tabline or a plugin.

JS Stand-in: tab pages as layouts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: gt and gT wrap around

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Vim tab page contain?

    A tab page is a layout of windows. Each window shows a buffer, which may be the same buffer in several places.
  2. 2. Which command gives you a full-screen copy of the current window that you can discard with :tabc?

    :tab split opens the current buffer in a new tab. Closing that tab leaves your original layout unchanged.
  3. 3. Which command sets a working directory for the current tab only?

    :tcd is tab-local, :lcd is window-local and :cd is global.
  4. 4. How do you jump to tab number 3?

    A count before gt selects that tab; :tabn 3 does the same.

Summary

  • A tab page is a layout of windows, not a file.
  • :tabe, :tabnew, :tab split create tabs; gt / gT / Ngt move; :tabc and :tabo close.
  • :tabmove reorders; :tcd gives a tab its own directory.
  • Use buffers for files, splits for side-by-side work and tabs for separate contexts.

Next: The Argument List and the Quickfix List