The vi editor ships with nearly every Linux and Unix system, which is why sysadmins still use it after almost 50 years. It runs in a terminal, uses keyboard shortcuts only, and works over SSH on any machine. The catch is that vi has two modes, and most beginners get stuck because typing letters does something other than what they expect.

This guide covers the commands you actually need to open a file, edit it, save it, and get out without losing your work.

Understanding vi Editor Modes Before Running Commands

vi opens in command mode by default. Keystrokes are read as instructions, not text. Press i and you switch to insert mode, where typing actually adds characters to the file. Press Esc and you go back to command mode.

A third mode, called last-line or ex mode, opens when you type a colon. This is where save, quit, and search-replace commands live.

If you ever lose track of which mode you’re in, hit Esc twice. You’ll be in command mode for sure.

How To Open a File With the vi Editor

Type vi filename at the shell prompt. If the file exists, vi loads it. If it doesn’t, vi creates a blank buffer and saves it under that name once you write changes.

You can also open vi without a filename and save it later using :w newfile.txt. To open a file in read-only mode, use vi -R filename or view filename. This protects you from accidental edits on system files. For editing privileged files like passwd or sudoers, the safer wrappers are vipw for password file edits and visudo for sudoers, both of which lock the file and validate syntax.

vi Insert Mode Commands for Adding Text

Insert mode is where you actually type. Six keys get you in, each placing the cursor differently:

CommandWhat it does
iInsert before the cursor
aAppend after the cursor
IInsert at the start of the current line
AAppend at the end of the current line
oOpen a new line below
OOpen a new line above

Press Esc when you’re done typing. The bottom-left status disappears, telling you you’re back in command mode.

Cursor Movement Commands in vi

vi uses h j k l for left, down, up, right. Arrow keys work on most modern terminals, but the letter keys are faster once your hands are on home row.

For bigger jumps, w moves forward one word and b moves back one word. 0 jumps to the start of the line, $ jumps to the end. gg takes you to the top of the file, G drops you at the bottom. To go to a specific line, type the number followed by G, like 42G for line 42.

Most movement commands accept a count prefix. 5j moves down five lines. 3w jumps three words forward. This pattern of count plus command is core to how vi gets work done quickly.

How To Save and Quit vi Editor

Save and exit commands all start with a colon. Press Esc first to make sure you’re in command mode, then type the colon.

CommandAction
:wWrite changes, stay in vi
:qQuit if no unsaved changes
:wqWrite and quit
ZZSave and quit (no colon needed)
:q!Quit without saving
:w newnameSave as a different filename

The :q! command saves you when you’ve made a mess. It throws away every change since the last write. Use it freely while learning.

Delete, Cut, Copy, and Paste in vi

Deletion in vi doubles as cut. Anything you delete goes into a buffer you can paste later with p.

x deletes the character under the cursor. dd removes the entire line. dw kills the word in front of the cursor. D wipes from the cursor to the end of the line. Prefix any of these with a number to repeat: 5dd deletes five lines.

To copy without deleting, use yy for the current line or yw for a word. Then move the cursor and press p to paste below or P to paste above. The u command undoes the last action, which is the single most useful key for beginners.

vi Command Frequency in Daily Editing

The chart below shows roughly how often common vi commands get used during typical config-file editing sessions, based on keystroke logging studies summarised in vim training material.

0% 10% 20% 30% 40% 35% 27% 20% 15% 12% 9% 6% i / a hjkl :wq dd / x u /search yy / p Estimated share of keystrokes per vi session

Search and Replace in vi Editor

To find text, type / followed by your search term and hit Enter. /error finds the next occurrence of “error” below the cursor. Press n to repeat the search forward, N to repeat backward. The ? command does the same thing in reverse, searching upward.

For substitution, the syntax is :s/old/new/ on the current line. Add g at the end to replace every match on the line: :s/old/new/g. To run it across the whole file, use :%s/old/new/g. Add c for confirmation before each replacement, like :%s/old/new/gc.

Useful vi Commands Beyond the Basics

A handful of extras will save you time once the basics feel automatic. :set number turns on line numbers, which makes navigation and debugging far easier. :set nonumber turns them off. The . key repeats your last change, which is brilliant for repetitive edits. % jumps between matching brackets and parentheses, useful when working with code. :!command runs a shell command without leaving vi, so :!ls shows you the directory contents. If your shell prefers a different default editor, the sensible-editor wrapper picks one based on your environment variables. For users coming from graphical editors like gedit, the muscle memory takes a week or two to build.

vi is also a symlink to Vim, the improved variant, on most distributions. Vim adds multiple undo levels, syntax highlighting, and split windows while keeping every command on this page intact.

FAQs

How do I exit vi if I’m stuck?

Press Esc twice to make sure you’re in command mode, then type :q! and hit Enter. This quits without saving any changes. Use :wq if you want to keep your edits.

What’s the difference between vi and Vim?

Vim is the improved version of vi with extra features like multi-level undo, syntax highlighting, and split windows. On most Linux systems, the vi command is actually a symlink that runs Vim.

Why doesn’t typing work when I open vi?

vi starts in command mode, where keys run commands instead of inserting text. Press i to enter insert mode, then type normally. Press Esc to return to command mode.

How do I delete a whole line in vi?

In command mode, press dd. To delete multiple lines, type the number first: 5dd removes five lines starting from the cursor. Press u immediately if you want to undo it.

Can I use the mouse in vi editor?

The classic vi editor does not support mouse input. All navigation and editing happens through keyboard commands. Vim has optional mouse support, which you can enable with :set mouse=a in command mode.

Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.