Vim - Working With Multiple Things
Vim - Working With Multiple Files, Buffers, Windows, and Tabs
Section titled “Vim - Working With Multiple Files, Buffers, Windows, and Tabs”Vim excels at managing multiple contexts simultaneously. This chapter explores how to work efficiently with multiple files, buffers (in-memory copies of files), windows (splits of the Vim screen), and tabs (collections of windows).
- Editing multiple files
- Managing multiple buffers
- Using multiple tabs
- Working with multiple windows (splits)
Editing Multiple Files
Section titled “Editing Multiple Files”You can open multiple files when starting Vim or open additional files during a session.
To open multiple files from the command line (each will typically open in its own buffer):
$ vim file1.txt file2.js file3.mdWithin Vim, to open another file in the current window (replacing the current buffer in that window):
| Command | Description |
|---|---|
| :edit | Load <filename> into the current window. Shorthand: :e <filename>. |
| :edit | Press Tab after :e to see file completions from the current directory. |
| :find | Search for <filename> in Vim’s path and open it. Useful for project files. See :help 'path'. |
If the current file has unsaved changes, Vim will prevent you from switching unless you save (:w) or force the command (e.g., :e! <filename>).
Managing Multiple Buffers
Section titled “Managing Multiple Buffers”A buffer is an in-memory copy of a file being edited. Every file you open is loaded into a buffer. You can have many buffers open but typically only one or a few are visible in windows at any time.
| Command | Description |
|---|---|
| :ls or :buffers | List all open buffers with their numbers and status. |
| :buffer <N_or_name> | Switch to buffer number N or buffer with a partial name. Shorthand: :b <N_or_name>. |
| :bnext or :bn | Go to the next buffer in the list. |
| :bprevious or :bp | Go to the previous buffer in the list. |
| :bfirst or :bf | Go to the first buffer in the list. |
| :blast or :bl | Go to the last buffer in the list. |
| :badd | Add <filename> to the buffer list without opening it in a window. |
| :bdelete <N_or_name> or :bd <N_or_name> | Unload buffer N or by name. If it has changes, you’ll be warned. Use :bd! to force. |
Example: If :ls shows buffer 3 is notes.txt:
:b 3 or :b notes would switch the current window to display notes.txt.
Output of :ls might look like:
1 %a "file1.txt" line 1 2 # "file2.js" line 0 3 "file3.md" line 0% indicates the current buffer, # the alternate buffer.
Using Multiple Tabs
Section titled “Using Multiple Tabs”Tabs (or tab pages) in Vim are collections of one or more windows. They allow you to manage different workspaces or sets of related files.
| Command | Description |
|---|---|
| :tabnew | Open <filename> in a new tab. If no filename, opens an empty buffer. Shorthand: :tabe <filename>. |
| :tabclose or :tabc | Close the current tab. If it’s the last tab, Vim may quit (depending on settings). |
:tabnext or :tabn or gt | Go to the next tab page. |
:tabprevious or :tabp or gT | Go to the previous tab page. |
| :tabfirst or :tabfir | Go to the first tab page. |
| :tablast or :tabl | Go to the last tab page. |
Ngt | Go to tab page number N (e.g., 3gt goes to the third tab). |
| :tabs | List all tab pages and the windows they contain. |
Visually, tabs are often shown at the top of the Vim window if you’re using a GUI version or a terminal that supports it, or if you have plugins like Airline.
Working with Multiple Windows (Splits)
Section titled “Working with Multiple Windows (Splits)”Windows (often called splits) allow you to view and edit multiple buffers simultaneously, or different parts of the same buffer, by dividing the Vim screen.
| Command | Description |
|---|---|
| :split | Split the current window horizontally and open <filename> in the new window. If no filename, duplicates the current buffer. |
| :vsplit | Split the current window vertically and open <filename>. If no filename, duplicates the current buffer. |
| :new | Create a new horizontal split with <filename> (or an empty buffer). |
| :vnew | Create a new vertical split with <filename> (or an empty buffer). |
Ctrl-w s or Ctrl-w Ctrl-s | Normal mode: Split current window horizontally (like :split). |
Ctrl-w v or Ctrl-w Ctrl-v | Normal mode: Split current window vertically (like :vsplit). |
Ctrl-w q or :quit or :q | Close the current window. Shorthand: :clo or Ctrl-w c. |
Ctrl-w o or :only | Make the current window the only one on the screen, closing all others in the tab. |
Ctrl-w <h,j,k,l> or Ctrl-w <ArrowKey> | Move cursor to the window in the specified direction (left, down, up, right). |
Ctrl-w w or Ctrl-w Ctrl-w | Cycle through open windows. |
Example: If you are editing main.c:
:sp utils.h - will split the screen horizontally, showing main.c in one window and utils.h in the other.
Resizing windows: Ctrl-w + (increase height), Ctrl-w - (decrease height), Ctrl-w > (increase width), Ctrl-w < (decrease width), Ctrl-w = (equalize sizes).