Blog / Linux/ Essential vi/vim Editor Commands: A Quick Reference Guide

Essential vi/vim Editor Commands: A Quick Reference Guide

vi/vim 编辑器常用命令详解与快速参考

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 :n for next, :N for previous.

Moving the Cursor

Basic Movement

  • h or : Left one character.
  • l or : Right one character.
  • k or : Up one line.
  • j or : Down one line.
  • 0: Move to line start.
  • ^: Move to first non-blank character.
  • $: Move to line end.

Word Movement

  • w or W: Next word start.
  • b or B: Previous word start.
  • e or E: Current/next word end.

Line Movement

  • gg: First line of file.
  • G: Last line of file.
  • nG or :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.
  • S or cc: 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$ or D: 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.
  • :wq or :x or ZZ: Save and quit.
  • :e filename: Edit another file.

Other Useful Commands

  • :set number or :set nu: Show line numbers.
  • :set nonumber or :set nonu: Hide line numbers.
  • :set ignorecase or :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 i to enter Insert mode, Esc to return to Normal mode, and : in Normal mode to enter Command mode. Beginners should first master basic cursor movement, insertion, saving, and quitting.

Post a Comment

Your email will not be published. Required fields are marked with *.