The VI Editor
vi is the editor guaranteed to exist on every Linux box — even rescue systems with nothing else installed. Master its three modes, the h j k l navigation keys, insert commands, editing operations, search and replace, and the save/quit/abort commands, then use split windows to work on multiple files. Ten minutes of practice here saves you from being stuck on a production server with only vi available.
What is this?
vi is a modal text editor that ships with virtually every Linux and Unix system. Instead of always typing directly into the document, you switch between a normal mode for navigation and editing commands, an insert mode for typing text, and a command line for file-level actions like saving, quitting, searching and replacing.
Why do you need it?
- vi is guaranteed to be present even on the most minimal servers, rescue disks and containers — a VPS with nothing installed still has vi.
- Modal editing means your hands never leave the home row: navigation, deletion and undo are single keys, not mouse movements or arrow-key chords.
- Every serious editor on Linux — vim, neovim, and even editor plugins in VSCode and JetBrains — implements vi keybindings, so the skill transfers everywhere.
- Editing config files on production servers is a core sysadmin task, and vi is the default $EDITOR on countless systems (crontab -e, git commit).
- For quick single-file edits it beats downloading, editing and re-uploading: one command, one session, done.
Mental model
A radio with separate controls for tuning and listening
vi with separate modes for command and insert
Most editors are like a phone: you are always typing. vi is like a radio with two dials — you switch to the tuning dial (normal mode) to move around and change the station, and to the volume dial (insert mode) to output sound. The mode you are in decides what a key press means.
How it works
- vi starts in normal mode, where every key is a command: h j k l move the cursor, x deletes a character, dd deletes a line.
- Pressing i, a, o or I, A, O switches to insert mode, where typed characters are written into the document exactly as pressed.
- Pressing Esc returns to normal mode, and typing : moves to the command line, where you run file-level commands like :w (save), :q (quit) and :s (search-and-replace).
- Search is also a normal-mode action: /pattern searches forward and ?pattern searches backward, with n and N repeating the search.
- vi is not vim: vim adds syntax highlighting, split windows, undo levels and plugins on top of vi, but the core modal vocabulary is identical.
The three vi modes and how you move between them.
Key commands
vim notes.txtOpen or create notes.txt. You start in normal mode.
iEnter insert mode before the cursor — now you can type text.
EscLeave insert mode and return to normal mode.
ddIn normal mode, delete the current line. It goes into the paste buffer.
uUndo the last change. Ctrl+r redoes what you undid.
:%s/dev/prod/gReplace every occurrence of dev with prod in the whole file.
6 substitutions on 4 lines
:wqWrite (save) the file and quit.
:q!Quit without saving — discards every change since the last :w.
Examples
A typical production edit: find a setting and change it
# Open the nginx configvim /etc/nginx/nginx.conf # In normal mode, search for "gzip"/gzip # Press n to jump to each match, n again for the next # Press i to insert, edit the value, then Esc to leave insert mode # Save and quit:wq # Or, if you made a mess: abandon everything:q!The whole workflow stays in one tool: open, search with /, move with n, edit in insert mode, and finish with :wq. The u key undoes mistakes, and :q! throws away all changes if you decide to start over — both are instant.
In production
- Editing config files on live servers: nginx.conf, sshd_config, cron jobs, sudoers — vi is the default editor everywhere.
- Quick single-change patches: fix a typo in a YAML file or bump a version number without leaving the terminal.
- Recovering broken systems: rescue environments ship vi when nothing else does, so you can fix fstab or GRUB from a live CD.
- Setting $EDITOR to vi (or vim) makes every tool that opens an editor — git, crontab, sudoedit — use the same familiar keybindings.
- Scripted edits via vi-style commands (sed, perl) share the same mental model, so search-and-replace skills map directly to automation.
Best practices
- Press Esc before anything else — if you are unsure what mode you are in, Esc is always safe.
- Make a habit of :w early and often; :q! is your escape hatch when a change goes wrong.
- Use /search before manually scrolling large configs — it is faster and finds every occurrence.
- Prefer g (global) with care: :%s/old/new/g changes everything; :%s/old/new/gc adds a confirmation prompt.
- Back up critical files before editing in production: cp nginx.conf nginx.conf.bak, then edit.
Common mistakes
- Typing text while still in normal mode — every key becomes a command and scrambles the file.
- Confusing :wq with :q! and accidentally quitting without saving (or vice versa).
- Using arrow keys habitually when h j k l are right on the home row and faster.
- Running :%s/old/new/g without checking, replacing strings that appear inside unintended words.
Common issues
E212: Can't open file for writingYou opened a read-only file (root-owned config as a normal user) and tried :w.
Run :w !sudo tee % to write with sudo, or close (:q!) and reopen with sudo vim file.
E32: No file nameYou ran :w on a new file that has never been given a name.
Use :w filename.txt to save it under a name, then :q.
Swap file "notes.txt.swp" already existsvim crashed or the same file is open in another session.
If the other session is yours, find it and :q it; otherwise open with vim -r notes.txt to recover the swap, or delete the .swp if you are sure no other editor has it open.
Debugging tips
- Esc, then u, undoes your last change; keep pressing u to walk back further.
- If the screen looks broken after a crash, run vi -r file.txt to recover the swap file.
- Run vim -R file.txt (read-only) when you only want to inspect a config you must not modify.
- Ctrl+g shows the file name, line number and position — useful before jumping around a 2,000-line config.
Interview checkpoint
Hands-on lab
- Open a file in vim and identify the active mode
- Navigate with h j k l, w, b, gg and G
- Enter insert mode, edit text and return to normal mode
- Search and replace, then save with :wq or abandon with :q!
Edit a file with vi and save your work
10 min · beginner
Quick quiz
You just opened a file in vim. Which key switches you into insert mode so you can type text before the cursor?
What does dd do when you are in normal mode?
The command :q! quits vim without saving any changes.
When vim file.txt first opens, which mode are you in?
Which keystroke searches forward for a pattern?
You made a series of messy edits to a production config and want to abandon everything, keeping the file exactly as it was on disk. What do you do?
Cheat sheet
iInsert mode — type before the cursoraInsert after the cursorEscBack to normal mode:Command mode (from normal mode)h j k lLeft, down, up, right0 / ^ / $Line start / first char / line endw / bNext / previous wordgg / GStart / end of file:5Jump to line 5xDelete the character under the cursorddDelete the current lineyyCopy (yank) the current linep / PPaste after / before the cursoru / Ctrl+rUndo / redo/patternSearch forward?patternSearch backwardn / NNext / previous match:s/old/new/gReplace in current line:%s/old/new/gReplace in the whole file:wSave:wqSave and quit:q!Quit without saving:e file2Open another file:split file2Horizontal split window:vsplit file2Vertical split windowCtrl+w wJump between split windows