Efficient Text Editing in Vi

0
123

Vi (and its enhanced version, Vim) is a powerful text editor in Unix-like systems. Here are some of the most popular and commonly used commands in Vi:

Basic Navigation

  • h, j, k, l: Move left, down, up, and right, respectively.
  • 0 (zero): Move to the beginning of the line.
  • ^: Move to the first non-blank character of the line.
  • $: Move to the end of the line.
  • w: Move to the beginning of the next word.
  • b: Move to the beginning of the previous word.
  • e: Move to the end of the current word.

Basic Editing

  • i: Insert mode before the cursor.
  • I: Insert mode at the beginning of the line.
  • a: Insert mode after the cursor.
  • A: Insert mode at the end of the line.
  • o: Open a new line below and enter insert mode.
  • O: Open a new line above and enter insert mode.
  • x: Delete the character under the cursor.
  • dd: Delete the current line.
  • D: Delete from the cursor to the end of the line.
  • dw: Delete the word from the cursor onward.
  • yy: Yank (copy) the current line.
  • p: Paste after the cursor.
  • P: Paste before the cursor.

Searching

  • /: Search forward for a pattern.
  • ?: Search backward for a pattern.
  • n: Repeat the search in the same direction.
  • N: Repeat the search in the opposite direction.

Undo/Redo

  • u: Undo the last change.
  • Ctrl + r: Redo the last undone change.

Visual Mode

  • v: Start visual mode (character-wise selection).
  • V: Start visual line mode (line-wise selection).
  • Ctrl + v: Start visual block mode (block-wise selection).

Saving and Exiting

  • :w: Save the file.
  • :q: Quit vi.
  • :wq or ZZ: Save the file and quit.
  • :q!: Quit without saving.

Other Useful Commands

  • :set nu: Show line numbers.
  • :set nonu: Hide line numbers.
  • :syntax on: Enable syntax highlighting (if using Vim).
  • :syntax off: Disable syntax highlighting.

These commands form the basis of efficient text editing in Vi, allowing for powerful navigation, editing, and file management within the text editor.