VI Commands to Remember
A compact, practical cheat sheet for using vi (and Vim). Learn the minimal commands you need to edit files safely on almost any Unix‑like system.
Modes and how to switch
vi is modal. Know which mode you are in before typing commands.
- Normal (Command) mode — navigate and issue commands.
- Insert mode — type text into the file.
- Command‑line mode — run file/editor commands (save, quit, search).
Enter Insert mode
- i — insert before the cursor
- a — append after the cursor
Return to Normal / Command mode
- Esc — return to Normal mode
- : — from Normal mode, enter Command‑line mode
Essential commands
The minimum you need to edit and save files.
Esc " return to Normal modei " insert before cursora " append after cursor:wq " write file and quit (save and exit):q! " quit without saving (discard changes)
Examples:
- Type i, make edits, press Esc, then :wq to save and exit.
- To abandon changes: press Esc then :q!.
Handy commands
Useful once you’re comfortable with the basics.
u " undo last changeCtrl+r " redo undone changedd " delete current lineyy " yank (copy) current linep " paste after cursor/pattern " search forward for patternn " go to next match
All of the above run from Normal mode — press Esc first if unsure.
Survival tips & suggested workflow
- Always press Esc before running a command to ensure you’re in Normal mode.
- Save frequently with :w to avoid losing work but remain in the editor.
- Use :w filename.bak to write a backup copy before risky edits.
- For multi‑step edits: navigate in Normal mode, use i or a to change text, then Esc and u to undo if needed.
- Keep this page or a printed cheat sheet near your keyboard until commands become muscle memory.
Where to go next
If you want to expand beyond the essentials, explore:
- Visual mode: v (select text visually)
- Word motions: w, b, e
- Find & replace: :%s/old/new/g
- Macros and registers for repetitive edits
Quick recap (one‑line cheat sheet)
Insert: i or a · Back to command: Esc · Save & exit: :wq · Quit no save: :q! · Scroll: Ctrl+u/Ctrl+d