Vi Editor Linux Terminal Cheat Sheet

Your comprehensive reference guide to Vi commands for efficient file editing in Linux terminal environments. Essential commands for developers and system administrators.

Understanding Vi Editor Fundamentals

Vi operates on a unique modal editing concept that distinguishes it from most modern text editors. The editor functions in three primary modes:

  • Command mode - The default mode for executing navigation and editing commands
  • Insert mode - Enables actual text entry and modification
  • Last-line mode - Provides file-level operations like saving and exiting

This modal approach offers significant advantages for experienced users who perform complex editing operations without leaving the keyboard. Commands that would require multiple keystrokes in graphical editors execute with single keystrokes in Vi.

Why Vi remains essential:

  • Universally available on every Linux and Unix system
  • Lightweight and starts instantly
  • Performs reliably even on heavily loaded systems
  • Essential for system recovery and remote server administration

For developers working in professional web development environments, mastering terminal-based tools like Vi significantly enhances productivity and workflow efficiency.

The Three Modes of Vi

Understanding modes is fundamental to effective Vi usage

Command Mode

Every keystroke triggers a command. Navigate with h,j,k,l. Execute operations like x (delete), dd (delete line), yy (yank line). Press ESC to return.

Insert Mode

Text appears directly in document. Enter with i (before cursor), a (after cursor), I (line start), A (line end), o (new line below), O (new line above).

Last-Line Mode

Access file operations with : prefix. :w saves, :q exits, :wq saves and exits, / searches forward, ? searches backward.

Essential Navigation Commands

Efficient navigation distinguishes proficient Vi users. The core navigation keys keep your hands in the home row position, eliminating the need to reach for arrow keys.

Core Movement Keys

CommandDescription
hMove left
jMove down
kMove up
lMove right

Word and Line Navigation

CommandDescription
wBeginning of next word
bBeginning of previous word
eEnd of next word
^First non-whitespace character
$End of current line
:nGo to line n
GEnd of file

Search Commands

CommandDescription
/textSearch forward for text
?textSearch backward for text
nNext search occurrence
NPrevious search occurrence

Use numeric prefixes to repeat movements: 10j moves down 10 lines, 5w moves forward 5 words.

These navigation skills complement other command-line productivity techniques that modern developers rely on daily.

Text Insertion and Modification

Vi provides multiple commands to enter insert mode, each positioning the cursor strategically for different scenarios.

Insertion Commands

CommandAction
iInsert before cursor
aInsert after cursor
IInsert at line beginning
AInsert at line end
oOpen new line below
OOpen new line above

Modification Commands

CommandAction
sSubstitute character (delete + insert)
SSubstitute entire line
rReplace single character
RReplace mode (overwrite)
c[motion]Change text (delete + insert)

Quick Tip

Press ESC to return to command mode after inserting text. This becomes automatic muscle memory with practice.

Deletion and Undo Operations

Deletion operations follow intuitive patterns that make them easy to remember and efficient to use.

Character and Line Deletion

CommandDescription
xDelete character under cursor
XDelete character before cursor
ddDelete entire current line
DDelete to end of current line
5xDelete 5 characters
5ddDelete 5 lines

Motion-Based Deletion

Combine d with movement operators:

CommandDescription
dwDelete to end of current word
d$Delete to end of line
d0Delete to beginning of line
dGDelete to end of file
d1GDelete to beginning of file

Undo and Repeat

CommandDescription
uUndo last change
.Repeat last change

The undo command (u) works sequentially, undoing multiple changes in reverse order.

Copy, Paste, and Buffer Operations

Vi uses the term "yank" for copy operations, reflecting its heritage from line-based editors.

Yank (Copy) Commands

CommandDescription
yy or YYank (copy) current line
5yyYank 5 lines
ywYank to end of word
y$Yank to end of line
y0Yank to beginning of line

Paste Commands

CommandDescription
pPaste after (below) cursor
PPaste before (above) cursor

Named Buffers

Vi supports multiple named buffers for sophisticated clipboard operations:

CommandDescription
"a5yyYank 5 lines to buffer a
"apPaste from buffer a
"bddDelete line to buffer b
"bpPaste from buffer b

Buffer names use lowercase letters (a-z), with each buffer maintaining independent content.

Search and Replace Operations

Search functionality enables quick navigation to specific text within files, while replace operations handle bulk modifications.

Search Commands

CommandDescription
/textSearch forward for text
?textSearch backward for text
nNext occurrence (same direction)
NPrevious occurrence (opposite direction)

Search Options

CommandDescription
:set ignorecaseCase-insensitive search
:set smartcaseCase-sensitive unless uppercase
:set incsearchIncremental search

Global Replace

CommandDescription
:%s/old/new/gReplace all in file
:%s/old/new/gcReplace all with confirmation
:1,10s/old/new/gReplace in lines 1-10
:%s/old/new/giReplace all, case-insensitive

The substitute command supports regular expressions for complex pattern matching.

File Operations and Exiting

File operations use last-line mode commands that appear at the bottom of the screen.

Save and Exit Commands

CommandDescription
:wSave file
:qQuit (if no unsaved changes)
:wq or :xSave and quit
:q!Quit without saving
ZZSave and quit (no colon needed)

Advanced File Operations

CommandDescription
:r filenameInsert file contents at cursor
:w filenameWrite buffer to new file
:enewOpen new empty buffer
:w !sudo tee %Write file requiring sudo

Line Positioning

CommandDescription
:0Go to beginning of file
GGo to end of file
1GGo to line 1
HFirst line of current screen
MMiddle line of screen
LLast line of screen

Special Characters and Line Operations

Vi provides visibility into normally hidden characters and powerful line-based operations.

Viewing Special Characters

CommandDescription
:set listShow special characters
:set nolistHide special characters

In list mode: ^I represents tabs, $ represents end of line

Repeat Command

CommandDescription
.Repeat last change

The dot (.) command is one of Vi's most powerful features. Any insertion, deletion, or modification becomes repeatable with a single keystroke.

Quick Reference Summary

  • Navigation: h,j,k,l, w,b,e, ^,$, /,?
  • Insertion: i,a,I,A,o,O
  • Deletion: x,X,dd,D,dw
  • Yank/Paste: yy,p,P
  • Search: /,?,n,N
  • File: :w,:q,:wq,:q!

These Vi skills integrate seamlessly with modern web development workflows, particularly when working with version control systems and configuration files.

Practical Applications and Use Cases

Real-world scenarios where Vi skills are essential

System Administration

Edit configuration files on production servers. Configure firewall rules, service settings, and user management files using Vi's universal availability.

Remote Server Management

Edit files through SSH connections. Vi's keyboard-only operation works perfectly in constrained environments with network latency.

Development Workflows

Quick code edits, commit message composition, and merge conflict resolution. Integrate naturally with git and other version control systems.

System Recovery

Essential for recovery scenarios. When graphical interfaces fail, Vi provides reliable editing capability for system repair and configuration.

Frequently Asked Questions

Master Your Development Workflow

Efficient text editing in the terminal is a skill that pays dividends throughout your career. The modal editing approach may feel unfamiliar at first, but becomes second nature with practice.