Vim Cheatsheet
Buffers, Windows, and Tabs
Use this Vim reference while you build software engineering projects, review code, or refresh the syntax you reach for most.
Buffers
A buffer is a file loaded into memory. Windows are viewports onto buffers, and tabs are collections of windows. Vim can hold dozens of buffers with only one window open.
| Command | Action |
|---|---|
:e file | Edit a file in the current window |
:e! | Reload the file, discarding changes |
:ls or :buffers | List every buffer |
:b 3 | Go to buffer 3 |
:b name | Go to a buffer by partial name |
:bn / :bp | Next / previous buffer |
:bf / :bl | First / last buffer |
Ctrl-^ or :b# | Toggle between the two most recent buffers |
:bd | Delete (close) the buffer |
:bd! | Close it, discarding changes |
:bufdo cmd | Run a command in every buffer |
:badd file | Load a file without switching to it |
:ls flag | Means |
|---|---|
% | The buffer in the current window |
# | The alternate buffer (Ctrl-^ target) |
a | Active: loaded and visible |
h | Hidden: loaded but not shown |
+ | Modified |
- | nomodifiable |
= | Read-only |
:set hidden " allow switching away from a modified buffer
Without hidden, Vim refuses to leave an unsaved buffer. Turning it on is one of the first things most people put in their config.
Windows (Splits)
| Command | Key | Action |
|---|---|---|
:sp | Ctrl-w s | Split horizontally |
:vs | Ctrl-w v | Split vertically |
:sp file | Split and open a file | |
:new | Ctrl-w n | Split with an empty buffer |
:clo | Ctrl-w c | Close this window |
:on | Ctrl-w o | Close every other window |
:q | Ctrl-w q | Quit this window |
| Key | Moves to |
|---|---|
Ctrl-w h/j/k/l | The window left / below / above / right |
Ctrl-w w | The next window, cycling |
Ctrl-w p | The previously focused window |
Ctrl-w t / Ctrl-w b | The top-left / bottom-right window |
| Key | Resizes or moves |
|---|---|
Ctrl-w = | Equalize all windows |
Ctrl-w _ | Maximize height |
Ctrl-w | | Maximize width |
Ctrl-w + / - | Grow / shrink height by one line |
Ctrl-w > / < | Grow / shrink width |
10 Ctrl-w + | Grow by 10 lines |
:resize 20 | Set height to 20 |
:vertical resize 80 | Set width to 80 |
Ctrl-w H/J/K/L | Move this window to the far left / bottom / top / right |
Ctrl-w r | Rotate the windows |
Ctrl-w x | Exchange with the next window |
Ctrl-w T | Move this window into its own tab |
:set splitright splitbelow " new splits open where you expect
Tabs
Tabs in Vim are workspaces, not files. Most people keep a handful of tabs and many buffers, not one tab per file.
| Command | Key | Action |
|---|---|---|
:tabnew | New empty tab | |
:tabnew file | New tab with a file | |
:tabe file | Same thing | |
:tabn | gt | Next tab |
:tabp | gT | Previous tab |
:tabfirst / :tablast | First / last tab | |
2gt | Go to tab 2 | |
:tabc | Close this tab | |
:tabo | Close every other tab | |
:tabs | List tabs and their windows | |
:tabm 0 | Move this tab to the front | |
:tabdo cmd | Run a command in every tab |
Finding Files
:e path/to/file " Tab completes :find file.txt " search along 'path' :set path+=** " let :find recurse from the cwd :e **/name<Tab> " fuzzy-ish completion with a recursive glob gf " open the file whose name is under the cursor Ctrl-w f " open it in a split gx " open the URL under the cursor externally :pwd " current directory :cd path " change it :lcd path " change it for this window only
| Netrw command | Action |
|---|---|
:E or :Explore | Browse the current file's directory |
:Sex / :Vex | Browse in a horizontal / vertical split |
:Rex | Return from netrw to the file you came from |
- (in netrw) | Go up a directory |
Enter | Open the entry |
% | Create a new file |
d | Create a directory |
D | Delete the entry |
R | Rename the entry |
I | Toggle the banner |
Netrw is built in, so it is what you get on a bare server. In a configured setup a fuzzy finder (fzf.vim, telescope) usually replaces it for opening files.
Sessions and Views
:mksession ~/work.vim " save windows, tabs, buffers, cwd :mksession! ~/work.vim " overwrite vim -S ~/work.vim " restore from the shell :source ~/work.vim " restore from inside Vim :mkview " save this window's folds and options :loadview " restore them :set sessionoptions-=options " do not bake options into sessions
The Arglist
The arglist is the set of files you passed on the command line, and it is the cleanest way to run one command over a known set of files.
| Command | Action |
|---|---|
:args | Show the arglist |
:args **/*.ts | Replace it with a glob |
:argadd file | Add a file |
:argdelete * | Clear it |
:next / :prev | Next / previous argument |
:first / :last | Jump to the ends |
:argdo cmd | Run a command on every argument |
:args `git diff --name-only main`
:argdo %s/\<oldName\>/newName/ge | update