Introduction to vi/vim
vi is a classic text editor in Unix/Linux systems, with its enhanced version vim (Vi IMproved) being more commonly used today. The following commands apply to both vi and vim.
Starting vi/vim
Opening Files
vi filename: Open or create a file, cursor at first line.vi +n filename: Open file, cursor at line n.vi + filename: Open file, cursor at last line.vi +/pattern filename: Open file, cursor at first match of pattern.vi -r filename: Recover a crashed file.vi file1 file2 …: Open multiple files; use:nfor next,:Nfor previous.
Moving the Cursor
Basic Movement
hor←: Left one character.lor→: Right one character.kor↑: Up one line.jor↓: Down one line.0: Move to line start.^: Move to first non-blank character.$: Move to line end.
Word Movement
worW: Next word start.borB: Previous word start.eorE: Current/next word end.
Line Movement
gg: First line of file.G: Last line of file.nGor:n: Go to line n.n+: Move down n lines.n-: Move up n lines.
Screen Movement
H: Top of screen.M: Middle of screen.L: Bottom of screen.
Scrolling
Ctrl + f: Scroll forward one screen.Ctrl + b: Scroll backward one screen.Ctrl + d: Scroll forward half screen.Ctrl + u: Scroll backward half screen.zz: Center current line on screen.
Inserting and Replacing Text
Enter Insert Mode
i: Insert before cursor.I: Insert at line beginning.a: Insert after cursor.A: Insert at line end.o: Open new line below.O: Open new line above.
Replace and Change
r: Replace single character.R: Enter replace mode (continuous).s: Delete character and insert.Sorcc: Delete line and insert.cw: Change to end of word.
Deleting Text
x: Delete character under cursor.X: Delete character before cursor.dd: Delete current line.ndd: Delete n lines.dw: Delete to next word start.d$orD: Delete to line end.d0: Delete to line start.
Search and Replace
Search
/pattern: Search forward for pattern.?pattern: Search backward for pattern.n: Repeat search same direction.N: Repeat search opposite direction.
Replace (Command Mode)
:s/old/new/: Replace first 'old' on line.:s/old/new/g: Replace all 'old' on line.:%s/old/new/g: Replace all 'old' in file.:n1,n2s/old/new/g: Replace between lines n1 and n2.
Copy, Paste, and Undo
yy: Yank (copy) current line.nyy: Yank n lines.p: Paste after cursor.P: Paste before cursor.u: Undo last change.Ctrl + r: Redo last undo.
File Operations and Exiting
:w: Write (save) file.:w filename: Save as filename.:q: Quit (if saved).:q!: Quit without saving.:wqor:xorZZ: Save and quit.:e filename: Edit another file.
Other Useful Commands
:set numberor:set nu: Show line numbers.:set nonumberor:set nonu: Hide line numbers.:set ignorecaseor:set ic: Ignore case in search.:!command: Execute shell command.:r filename: Insert file contents.:r !command: Insert command output.
Tip: vi has three basic modes: Normal, Insert, and Command. Most operations occur in Normal mode. Press
ito enter Insert mode,Escto return to Normal mode, and:in Normal mode to enter Command mode. Beginners should first master basic cursor movement, insertion, saving, and quitting.