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.
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
| Command | Description |
|---|---|
| h | Move left |
| j | Move down |
| k | Move up |
| l | Move right |
Word and Line Navigation
| Command | Description |
|---|---|
| w | Beginning of next word |
| b | Beginning of previous word |
| e | End of next word |
| ^ | First non-whitespace character |
| $ | End of current line |
| :n | Go to line n |
| G | End of file |
Search Commands
| Command | Description |
|---|---|
| /text | Search forward for text |
| ?text | Search backward for text |
| n | Next search occurrence |
| N | Previous 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
| Command | Action |
|---|---|
| i | Insert before cursor |
| a | Insert after cursor |
| I | Insert at line beginning |
| A | Insert at line end |
| o | Open new line below |
| O | Open new line above |
Modification Commands
| Command | Action |
|---|---|
| s | Substitute character (delete + insert) |
| S | Substitute entire line |
| r | Replace single character |
| R | Replace 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
| Command | Description |
|---|---|
| x | Delete character under cursor |
| X | Delete character before cursor |
| dd | Delete entire current line |
| D | Delete to end of current line |
| 5x | Delete 5 characters |
| 5dd | Delete 5 lines |
Motion-Based Deletion
Combine d with movement operators:
| Command | Description |
|---|---|
| dw | Delete to end of current word |
| d$ | Delete to end of line |
| d0 | Delete to beginning of line |
| dG | Delete to end of file |
| d1G | Delete to beginning of file |
Undo and Repeat
| Command | Description |
|---|---|
| u | Undo 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
| Command | Description |
|---|---|
| yy or Y | Yank (copy) current line |
| 5yy | Yank 5 lines |
| yw | Yank to end of word |
| y$ | Yank to end of line |
| y0 | Yank to beginning of line |
Paste Commands
| Command | Description |
|---|---|
| p | Paste after (below) cursor |
| P | Paste before (above) cursor |
Named Buffers
Vi supports multiple named buffers for sophisticated clipboard operations:
| Command | Description |
|---|---|
| "a5yy | Yank 5 lines to buffer a |
| "ap | Paste from buffer a |
| "bdd | Delete line to buffer b |
| "bp | Paste 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
| Command | Description |
|---|---|
| /text | Search forward for text |
| ?text | Search backward for text |
| n | Next occurrence (same direction) |
| N | Previous occurrence (opposite direction) |
Search Options
| Command | Description |
|---|---|
| :set ignorecase | Case-insensitive search |
| :set smartcase | Case-sensitive unless uppercase |
| :set incsearch | Incremental search |
Global Replace
| Command | Description |
|---|---|
| :%s/old/new/g | Replace all in file |
| :%s/old/new/gc | Replace all with confirmation |
| :1,10s/old/new/g | Replace in lines 1-10 |
| :%s/old/new/gi | Replace 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
| Command | Description |
|---|---|
| :w | Save file |
| :q | Quit (if no unsaved changes) |
| :wq or :x | Save and quit |
| :q! | Quit without saving |
| ZZ | Save and quit (no colon needed) |
Advanced File Operations
| Command | Description |
|---|---|
| :r filename | Insert file contents at cursor |
| :w filename | Write buffer to new file |
| :enew | Open new empty buffer |
| :w !sudo tee % | Write file requiring sudo |
Line Positioning
| Command | Description |
|---|---|
| :0 | Go to beginning of file |
| G | Go to end of file |
| 1G | Go to line 1 |
| H | First line of current screen |
| M | Middle line of screen |
| L | Last line of screen |
Special Characters and Line Operations
Vi provides visibility into normally hidden characters and powerful line-based operations.
Viewing Special Characters
| Command | Description |
|---|---|
| :set list | Show special characters |
| :set nolist | Hide special characters |
In list mode: ^I represents tabs, $ represents end of line
Repeat Command
| Command | Description |
|---|---|
| . | 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.
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.