Vim is the modal editor installed on practically every Linux server, so it is what opens when you edit a file over SSH. This sheet covers the shortcuts people actually look up: saving and quitting, copy and paste, search and replace, motions, splits, and macros.
Press Esc first: the commands only work from normal mode.
Key
Action
Save
or
Save and quit ( skips the write when nothing changed)
Save and quit, no colon needed
Quit; refuses if there are unsaved changes
or
Quit and throw the changes away
Save every open file
Quit every window ( discards changes)
Save everything and quit
Save a copy under another name, keep editing this one
Write a root-owned file you opened without sudo (then )
Reload the file from disk, discarding your edits
Copy and Paste
Vim calls copy yank and paste put. Deletes cut into the same place, so then moves a line.
Key
Action
Yank the current line ( yanks five)
Yank to the start of the next word
Yank to the end of the line
Paste after the cursor, or below the line for line yanks
Paste before the cursor or above the line
select, then
Yank the selected lines
Yank to the system clipboard (needs , see Gotchas)
Paste from the system clipboard
Yank the whole file to the system clipboard
Paste the last yank, ignoring deletes since
Yank the line into register a ( pastes it)
Show what every register holds
Duplicate the line ( puts the copy above)
Swap two characters ( swaps two lines)
Delete
Key
Action
Delete the character under the cursor
Delete the line ( deletes five)
Delete to the start of the next word
Delete the whole word under the cursor
or
Delete to the end of the line
Delete to the start of the line
/
Delete to the top / bottom of the file
Delete every line in the file
Delete lines 5 through 10
Delete every line matching a pattern
Delete the rest of the word and start typing (change)
or
Clear the line and start typing
Clear to the end of the line and start typing
Join the next line onto this one
Search and Replace
Key
Action
Search forward ( next match, previous)
Search backward
/
Search for the word under the cursor, forward / back
Turn off the match highlighting until the next search
Replace every occurrence in the file
Same, asking before each one (, , for all, quits)
Replace the first occurrence on this line
Replace only in lines 5 to 12
Case-insensitive replace
Reuse the last search as the pattern
Select with , then
Replace inside the selection (Vim fills in )
Regex
Vim's default pattern syntax needs backslashes where most languages do not. at the front of a pattern (very magic) makes it behave like the regex you already know.
Pattern
Matches
Very magic: work without backslashes for the rest of the pattern
Any character ( for a literal dot)
Digit, word character, whitespace ( invert them)
Zero or more of the previous item
/
One or more / optional (no backslash after )
Between two and five of the previous item
/
Start / end of the line
/
Start / end of a word
Capture group, written back as in the replacement
/
Start / end the match here; the rest is context, not replaced
/
Force this search case-insensitive / case-sensitive
in the replacement
Use instead; inserts a null byte
:%s/\v(\w+)\s+(\w+)/\2 \1/g " swap the first two words on every line
:%s/\v<(foo|bar)>/baz/g " whole words foo or bar, nothing longer
:%s/\vhttps?:\/\/\S+//g " strip URLs
:g/\v^\s*$/d " delete every blank line
:%s/error\zs.*// " keep the word error, drop the rest of the line
Undo and Redo
Key
Action
Undo ( undoes five changes)
Redo
Undo everything done to the last edited line
Repeat the last change somewhere else
The file as it was five minutes ago
Forward in time again
/
Step through the undo history, including abandoned branches
Navigation and Motions
A count multiplies any motion: moves five lines down, three words forward. Motions also feed , , and , so deletes three words.
Key
Action
Left, down, up, right
/
Start of the next / previous word
End of the word
/ /
Start of the line / first character / end of the line
/
Top / bottom of the file
or
Go to line 42
/
Previous / next blank line (paragraph)
/
Half a page down / up
/
A full page down / up
Top, middle, bottom of the screen
Scroll so the cursor line is centered ( top, bottom)
/
Jump onto the next / previous x on this line ( repeats, reverses)
Jump to just before the next x
Jump between matching , ,
/
Back / forward through your jump history
then
Set mark a, jump back to that exact spot ( for its line)
Modes
Vim starts in normal mode, where keys are commands, not text. Esc always gets you back there.
Key
Action
/
Insert before / after the cursor
/
Insert at the start / end of the line
/
Open a new line below / above and insert
or
Back to normal mode
Visual mode (select by character)
Visual line mode
Visual block mode (select a column)
Replace mode: type over what is there
Replace one character with x, staying in normal mode
Command-line mode for , , and friends
(in insert)
Delete the word you just typed
(in insert)
Run one normal-mode command, then keep typing
Visual Mode and Visual Block
Key
Action
/ /
Select by character / line / block
Select the whole file
Jump to the other end of the selection
Reselect the last selection
/ /
Yank, delete, or change the selection
/
Indent / outdent the selection
/ /
Toggle / lower / upper case
, select, , text,
Insert the same text on every selected line
, , , text,
Append text at the end of every selected line
, select, , then a character
Replace every selected character with it
Splits
All the window commands live on : press it, release, then the key.
Key
Action
or
Split top and bottom
or
Split left and right
Open another file in a new split
Move to the split in that direction
Cycle through the splits
Make all splits the same size
/
Taller / shorter ( and for wider and narrower)
Close this split ( does the same)
or
Close every split except this one
Rotate the splits
Move this split into its own tab
Buffers and Tabs
Every open file is a buffer; splits and tabs are just views onto them.
Key
Action
Open a file (Tab completes the path)
List the open buffers with their numbers
or
Jump to buffer 2, or by partial file name
/
Next / previous buffer
Flip between the last two buffers
Close the buffer
Open a file in a new tab
/
Next / previous tab ( for tab 2)
Close the tab
Macros
Record keystrokes once, replay them everywhere. Start the recording with a motion like so it works from any column, and end it with so replays walk down the file.
Key
Action
Start recording into register a (the status line shows recording)
Stop recording
Play the macro
Replay the last-played macro
Play it 25 times
Select lines, then
Run the macro once on every selected line
Record more onto the end of macro a
Show the recorded keystrokes
Comment and Indent
Plain Vim has no comment toggle; visual block fills the gap. Neovim 0.10+ ships to toggle a comment (Vim 9.1 gets it after ).
Key
Action
, select lines, ,
Comment the selected lines with
, select the column,
Uncomment them
Comment lines 10 to 50 ( undoes it)
/
Indent / outdent the line ( for five lines)
Indent the paragraph
Auto-indent the line
Auto-indent the whole file
/ (in insert)
Indent / outdent while typing
In visual mode indents one level and drops the selection; use for two levels or to hit it again.
Transform Text
runs the file, or a range of it, through an outside command and puts the output back. That is how Vim formats JSON without a plugin.
Command
What it does
Sort the file ( drops duplicates, sorts numerically)
Sort only lines 5 to 20 (or select them first, then )
/
Move this line down / up ( sends it to the top)
/
Upper-case / lower-case the word ( for the whole line)
Flip the case of the line
/
Add one to / subtract one from the number under the cursor
(visual)
Turn a column of identical numbers into 1, 2, 3...
Word, line and character count for the file or the selection
Reformat the buffer as pretty JSON
Line the columns up
Replace the file with the output of any shell pipeline
Insert a command's output at the cursor ( inserts a file)
Grep and Quickfix
The quickfix list holds a set of file-and-line results. fills it from a search, and then runs an edit on every hit, which is how a replace crosses more than one file.
Command
What it does
Search every matching file below this directory
The same through the system grep, which is faster on big trees
/
Open / close the results window (Enter jumps to a hit)
/
Next / previous result without leaving the file
/
First / last result
Run a replace on every result and save each file
Same, but once per file instead of once per match
/
Step back and forward through earlier result lists
Text Objects
Text objects target the thing around the cursor: an operator (, , , ), then for inside or for around, then the object. stops at the delimiters, takes them too.
Key
Action
/
Delete the word / the word plus the space after it
Change what is inside the quotes
Delete what is inside the parentheses
Delete the block, braces included
Change what is inside an HTML or XML tag
/
Delete the paragraph
Yank what is inside the square brackets
Select what is inside the braces (repeat to grow outward)
Folding
Folding collapses blocks so long files scan faster. The default is manual; folds by indentation, which suits YAML and Python.
Key
Action
Toggle the fold under the cursor
/
Open / close a fold
/
Open / close every fold in the file
+ motion
Create a fold, e.g. folds the paragraph (manual method)
Delete the fold; the text stays
/
Jump to the next / previous fold
Fold by indent ( and also exist)
Vimdiff
opens both files side by side with the differences highlighted. and move a change between them, which is the fast way to reconcile a config against its or copy.
Key
Action
Open two (or three) files side by side, differences highlighted
The same thing, shorter
/
Jump to the next / previous change
Pull the other side's version of this change into the current file
Push this change into the other file
Redraw the highlighting after editing
Add the current split to the diff (run it in each window)
Diff every split at once
Turn the diff off here ( in every window)
/
Show more / less unchanged context around the changes
Ignore whitespace-only differences
Settings
changes a setting for this session only. The same line without the colon in makes it stick. Neovim reads instead.
Command
What it does
Line numbers ( off, toggles)
Numbers counted from the cursor, so is easy to aim
Searches ignore case until you type a capital
Highlight every match, jump as you type
Spaces instead of tabs, four wide
Keep the previous line's indent on a new line
Show tabs and trailing spaces
Let long lines run off the screen instead of wrapping
Syntax highlighting
Stop auto-indent stair-stepping a terminal paste ( after)
Let Vim use the mouse (breaks terminal selection, see Gotchas)
Let backspace delete past where the insert started
Switch theme; Tab completes the installed names
Show a setting's value and which file set it
Print the config file this Vim actually loaded
Reload the config without restarting Vim
A usable starting :
" ~/.vimrc
set number relativenumber
set ignorecase smartcase incsearch hlsearch
set expandtab tabstop=4 shiftwidth=4 autoindent
set list listchars=tab:>-,trail:.
syntax on
Plugins
Vim 8 and later load plugins on their own, so a manager is optional: anything dropped in loads at startup.
Command
What it does
Install a plugin; it loads next time Vim starts
Install it dormant instead, for to load on demand
Load an optional plugin now ( for Vim 9.1's commenting)
Rebuild help tags so finds the new plugin's docs
List every script Vim has loaded, in order
Start without plugins to see whether one is the problem
With vim-plug, list the plugins between and in , then ; upgrades them and removes the ones you deleted from the list.
Help
Vim carries its own manual, and the help topic names are not always what you would guess.
Command
What it does
Open the manual ( closes the help split)
Help for a normal-mode command
Help for a colon command; the colon is part of the topic
Help for a setting; the quotes mark it as an option
Help for an insert-mode key ( visual, command line)
Help for an error code, straight from the message Vim printed
Search the whole manual ( for the next hit)
/
Follow a help link / go back
The built-in 30-minute tutorial, run from the shell
Which build this is and which features it was compiled with
Gotchas
If typing produces commands instead of text, or text instead of commands, check the mode. Esc puts you in normal mode from anywhere; the tag at the bottom says you are typing text.
Backspace that refuses to delete past the point where you started typing means Vim is in vi-compatible mode. restores the behaviour everyone expects; put the line in so it sticks.
Vim has no multiple cursors. Visual block covers the column case, and for everything else make the change once, then repeat it with or record it as a macro.
A message in the status line means you pressed by accident and every keystroke is being recorded. Press once to stop; nothing is harmed.
silently does nothing on a Vim built without clipboard support, which includes most server installs ( shows ). Over SSH the clipboard is on the wrong machine anyway: select with the mouse in your terminal, or run the session inside tmux and use its copy mode.
On macOS the system in is built without clipboard support, so does nothing there either. Pipe instead: copies the file and copies a visual selection. gets a build with .
makes Vim grab the mouse, so terminal selection stops working. Hold Shift while dragging to select with the terminal again.
with a swap file warning means a crashed or still-open session left a file behind. Recover the unsaved changes with , then delete the ; if another terminal really has the file open, edit it there instead.
Pasting into an old Vim over SSH can cascade the indentation. before pasting and after fixes it; Vim 8+ handles bracketed paste on its own.
Vim FAQ
What are the basic Vim commands?
Ten carry most of a session: i to start typing, Esc to stop, :w to save, :q to quit, dd to delete a line, yy to copy one, p to paste, u to undo, / to search, and gg and G for the top and bottom of the file. The habit that matters more than the list is leaving insert mode the moment you stop typing, because every other key only works from normal mode. Vim also ships its own tutorial: run vimtutor from the shell for a 30-minute walkthrough on a scratch copy you cannot damage.
How do I save and exit Vim?
Press Esc first, then type :wq and Enter to save and quit, or ZZ without the colon. :q! quits and throws the changes away. :x and ZZ do the same job as :wq with one difference: they skip the write when nothing changed, so the file keeps its old timestamp and make or a file watcher stays quiet. If typing :wq just prints the letters, you were still in insert mode; press Esc and try again. If the status line says recording, you pressed q earlier; press q once to stop, then quit. If Vim refuses with E45 (readonly), you opened a root-owned file without sudo: force it with :w !sudo tee % and then :q!, or quit and reopen with sudo.
How do I copy and paste in Vim?
Inside Vim, yy copies a line and p pastes it. For the system clipboard, yank with "+y ("+yy for a line) and paste with "+p. That needs a Vim built with clipboard support: vim --version shows +clipboard or -clipboard, and most server builds and the macOS system vim have the minus. Over SSH there is a second catch, since the + register lives on the server, not your machine. The practical fixes are selecting with the mouse in your terminal (hold Shift if Vim has mouse mode on), piping to a local tool such as :w !pbcopy on macOS, or OSC 52 support in Vim 9.1 and Neovim, which sends yanks through the terminal to your own clipboard.
How do I search and replace in Vim?
:%s/old/new/g replaces every occurrence in the file; add c to confirm each one (:%s/old/new/gc) and i to ignore case. Without the %, the replace only touches the current line, which is the usual reason it looks like nothing happened. The delimiter does not have to be a slash: :%s#/var/www#/srv/site#g avoids escaping paths. After searching with /, :%s//new/g reuses that search as the pattern.
What is the difference between vi and Vim?
Vim is a rewrite of vi that adds multi-level undo, syntax highlighting, visual mode, macros, and split windows. On most Linux systems typing vi starts Vim anyway, through a symlink or through the vim.tiny build that Debian and Ubuntu install by default, and that tiny build is where people meet one-level undo and arrow keys that insert letters instead of moving. :version names the build and lists which features it was compiled with; if the first line says Small or Tiny, install the full vim package. Neovim is a later fork of Vim that keeps the same keys and commands, so everything on this page works there too; what it changes is the config file, the plugin API, and the defaults. Everything here assumes the full Vim build.