vi
Modal terminal text editor
By CMD Script Team · 4 min read · Last updated
vi [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
+NUM | Open the file with the cursor on line NUM |
+/pattern | Open the file with the cursor on the first line matching pattern |
-R | Open the file in read-only mode |
-r | Recover a file from a swap file left after a crash |
-c COMMAND | Execute an Ex command immediately after opening the file |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
vi is a modal terminal text editor: keystrokes mean different things depending on the
current mode. In normal mode keys are commands (move, delete, copy, search); in
insert mode keys are typed as literal text. This split lets experienced users edit
extremely quickly without ever reaching for a mouse, at the cost of a steeper learning
curve than a simple graphical editor. vi (or its vastly more common modern
implementation, Vim) is preinstalled on nearly every Unix-like system, making it the one
editor you can almost always count on being available.
Beginner examples
vi file.txt— open a file for editing- Press
ito enter insert mode and start typing - Press
Escto return to normal mode - Type
:wqand press Enter to save and quit - Type
:q!and press Enter to quit without saving
vi ~/.bashrc
Advanced examples
- Delete the current line:
dd(in normal mode); delete 3 lines:3dd - Copy (yank) the current line and paste it below:
yythenp - Search forward for text and jump to the next match:
/patternthenn - Open a file with the cursor already on line 42:
vi +42 script.sh - Substitute text on every line of the file (Ex command):
:%s/old/new/g
vi +/TODO notes.txt
Common mistakes
- Typing text while still in normal mode, which triggers a flurry of unexpected commands
instead of inserting characters — always confirm you're in insert mode (often shown as
-- INSERT --at the bottom) before typing content. - Trying to quit with
Ctrl+Cor closing the terminal instead of:q/:wq, leaving swap files behind or losing edits. - Forgetting
Escbefore typing a command like:wq, so the colon and letters get inserted as literal text instead of being interpreted as a command. - Panicking and force-quitting after an accidental large deletion instead of pressing
uto undo.
Tips
- Press
uto undo the last change, andCtrl+Rto redo — essential for recovering from mistakes without quitting. - Use
:set numberto show line numbers, which makes navigating with:NUM(jump to line) much easier. dwdeletes a word,xdeletes a single character, and.repeats the last change — small building blocks that combine into fast edits.
Best practices
- Learn to stay in normal mode by default and only enter insert mode briefly to type, rather than staying in insert mode and using arrow keys like a basic text editor — that's where vi's efficiency comes from.
- Use
:wq(orZZ) deliberately rather than muscle-memoryCtrl+S, which does nothing useful in a terminal editor and can freeze the terminal (Ctrl+Q resumes it). - When editing config files on remote servers over SSH,
vi/Vim is worth learning well precisely because it's reliably available even on minimal systems where nothing graphical exists.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Editing a server configuration file over an SSH session where no graphical editor is available.
- Making a quick fix to a script during an incident, where speed and no dependency on a GUI matter.
- Editing commit messages, crontabs, or other files that many Unix tools open in
vi(or the system's$EDITOR) by default.
Common interview questions
- What's the difference between normal mode and insert mode in vi? In normal mode, keystrokes are interpreted as commands (movement, deletion, copying); in insert mode, keystrokes are typed as literal text into the file.
- How do you save and quit vi?
:wqsaves and quits;:q!quits and discards unsaved changes;ZZis a normal-mode shortcut equivalent to save-and-quit. - What do dd, yy, and p do in vi?
dddeletes the current line,yycopies (yanks) the current line, andppastes the most recently deleted or yanked text after the cursor.
Frequently Asked Questions
How do I save and quit vi?
From normal mode, type :wq and press Enter to save and quit, or :x which saves only if changes were made. Use ZZ (capital, in normal mode, no colon) as a shortcut for the same thing.
How do I quit without saving?
From normal mode, type :q! and press Enter. The ! forces the quit even though there are unsaved changes.
How do I switch between normal mode and insert mode?
Press i (or a, o, and others) to enter insert mode and start typing text. Press Esc to return to normal mode, where keystrokes are commands rather than text.
What do dd, yy, and p do?
dd deletes (cuts) the current line, yy yanks (copies) the current line, and p pastes whatever was last deleted or yanked below the cursor. Prefixing with a number repeats it, e.g. 3dd deletes three lines.
Cheat sheet
Download a quick-reference cheat sheet for vi.