Vi is a text editor that runs in your terminal, and its cheat sheet is a list of its commands. The core idea: vi has two modes — command mode (where keys do actions) and insert mode (where keys type text). Press i to start typing, Esc to stop, and :wq to save and quit.
Start and Stop Vi Without Losing Work
Open a file with vi filename. If the file doesn’t exist, vi creates it when you save.
:q— quit (only works if you haven’t made changes):q!— quit without saving (discards changes):w— save (write) the file:wq— save and quitZZ— save and quit (same as:wq, but no colon)
If you get stuck, press Esc once or twice to return to command mode. Then try :q! to escape safely.
Move Around: Cursor Keys and Shortcuts
In command mode, the arrow keys work on most systems, but the classic keys are faster once you learn them.
h— leftj— downk— upl— right0— start of line$— end of linew— next wordb— previous wordG— go to last linegg— go to first lineCtrl+d— scroll down half a screenCtrl+u— scroll up half a screen
To jump to a specific line, type the line number then G. For example, 42G goes to line 42.
Switch to Insert Mode and Type Text
To start typing, you must enter insert mode. Press one of these keys from command mode:
i— insert before the cursora— append after the cursoro— open a new line below and insertO— open a new line above and insert
Once you’re typing, Esc returns to command mode. That’s the basic loop: i to type, Esc to stop.
Delete, Copy, and Paste Text
These commands work in command mode. They act on the current character, word, or line.
x— delete character under cursordd— delete the whole linedw— delete from cursor to end of wordyy— copy (yank) the whole lineyw— copy the wordp— paste after the cursor (or below the line)P— paste before the cursor (or above the line)
You can repeat any command with a number. For example, 3dd deletes three lines, and 2yy copies two lines.
Undo and Search — The Two Lifesavers
Made a mistake? u undoes the last change. Press it repeatedly to undo more. To redo, use Ctrl+r.
To search for text, type / followed by the word and press Enter. For example, /error finds the next occurrence of “error”. Press n to jump to the next match, and N to go back.
To find and replace, use the substitute command:
:%s/old/new/g
This replaces every “old” with “new” in the whole file. The g means global (all occurrences on each line).
Where People Get Stuck: Modes and Quitting
The most common mistake is trying to type while still in command mode. If you press letters and nothing appears, you’re in command mode — press i to start typing.
The second most common mistake is trying to quit with :q when you have unsaved changes. Vi will say “No write since last change”. You have three choices: save with :w then quit, force quit with :q!, or save and quit with :wq.
If you ever get lost, remember: Esc always brings you back to command mode. From there, :q! is your emergency exit.
Worked Example: Edit a File from Start to Finish
Let’s say you want to change “hello” to “goodbye” in a file called note.txt.
- Open the file:
vi note.txt - Search for “hello”: type
/helloand pressEnter - Replace it: type
:%s/hello/goodbye/gand pressEnter - Save and quit: type
:wqand pressEnter
That’s it. The file now has “goodbye” in place of every “hello”.
Vi for Large Files and Repetitive Edits
If you’re editing a long file, use Ctrl+f (forward a full screen) and Ctrl+b (backward a full screen) to move faster than scrolling.
To repeat the last command, press . (dot). For example, if you just deleted a line with dd, pressing . deletes the next line too. This works for almost any command.
To change a word, use cw (change word). It deletes the word and puts you in insert mode so you can type the replacement immediately. Press Esc when done.
Check Your Work: Viewing and Verifying Changes
After editing, you can see your changes without leaving vi. Press Ctrl+g to show the file name and line count at the bottom. That helps you confirm you’re in the right file.
To see the current line number, type : then nu and press Enter. It shows the line number and the content.
If you want to undo everything since you opened the file, type :e! — this reloads the file from disk, discarding all changes. Use it only if you’re sure.
Vi Cheat Sheet for Quick Reference
Here’s a compact list to keep open while you practice:
i— insert modeEsc— back to command mode:wq— save and quit:q!— quit without savingdd— delete lineyy— copy linep— pasteu— undo/word— searchn— next search match
Keep this list handy. The more you use vi, the more natural these keys become.
Related
- Chess for Beginners Cheat Sheet: Rules and Moves
- Flat Sheet Under or Over Blanket: Which Way Works
- How to Fold Origami from a Single Sheet
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).