Linux
Beginner 12 min read Linux

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.

vivimtext editorvi modesnormal modeinsert modecommand modeh j k lsearch and replacesplit windows:wq:q!linux
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A radio with separate controls for tuning and listening

Think of

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.

Mechanics

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.

Open a file
Normal mode
Insert mode
Command line
Save and exit
Reference

Key commands

vim notes.txt

Open or create notes.txt. You start in normal mode.

i

Enter insert mode before the cursor — now you can type text.

Esc

Leave insert mode and return to normal mode.

dd

In normal mode, delete the current line. It goes into the paste buffer.

u

Undo the last change. Ctrl+r redoes what you undid.

:%s/dev/prod/g

Replace every occurrence of dev with prod in the whole file.

6 substitutions on 4 lines
:wq

Write (save) the file and quit.

:q!

Quit without saving — discards every change since the last :w.

Hands-on

Examples

A typical production edit: find a setting and change it

A typical production edit: find a setting and change it
# Open the nginx config
vim /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.

Real world

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.
Guidelines

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.
Watch out

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.
Root cause analysis

Common issues

Error
E212: Can't open file for writing
Root cause

You opened a read-only file (root-owned config as a normal user) and tried :w.

Fix

Run :w !sudo tee % to write with sudo, or close (:q!) and reopen with sudo vim file.

Error
E32: No file name
Root cause

You ran :w on a new file that has never been given a name.

Fix

Use :w filename.txt to save it under a name, then :q.

Error
Swap file "notes.txt.swp" already exists
Root cause

vim crashed or the same file is open in another session.

Fix

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.

When things break

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.
Interviews

Interview checkpoint

Practice

Hands-on lab

Edit a file with vi and save your work
10 min · beginner
In progress
Objectives
  • 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

0/6 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/6
1

You just opened a file in vim. Which key switches you into insert mode so you can type text before the cursor?

2

What does dd do when you are in normal mode?

3

The command :q! quits vim without saving any changes.

4

When vim file.txt first opens, which mode are you in?

5

Which keystroke searches forward for a pattern?

6

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?

Quick reference

Cheat sheet

Modes
iInsert mode — type before the cursor
aInsert after the cursor
EscBack to normal mode
:Command mode (from normal mode)
Navigation
h j k lLeft, down, up, right
0 / ^ / $Line start / first char / line end
w / bNext / previous word
gg / GStart / end of file
:5Jump to line 5
Editing
xDelete the character under the cursor
ddDelete the current line
yyCopy (yank) the current line
p / PPaste after / before the cursor
u / Ctrl+rUndo / redo
Search & replace
/patternSearch forward
?patternSearch backward
n / NNext / previous match
:s/old/new/gReplace in current line
:%s/old/new/gReplace in the whole file
Files & windows
:wSave
:wqSave and quit
:q!Quit without saving
:e file2Open another file
:split file2Horizontal split window
:vsplit file2Vertical split window
Ctrl+w wJump between split windows
Go deeper

Official documentation