Friday, 21 March 2025

Unix: Vi Editor Commands (CheatSheet)

 Opening and Exiting vi

  • vi filename   ->   Open a file
  • :q!   ->   Quit without saving
  • +wq!   ->   Save and quit
Switching Modes
  • esc   ->  exit to command mode
  • i   ->   insert mode (insert before cursor)
  • I   ->  insert at beginning of line
  • a.  ->  append after cursor
  • A  ->  append at end of line
  • o   ->  open a new line below
  • O  ->  open a new line above
Navigation
  • h   ->  move left
  • l    ->  move right
  • j    ->  move down
  • k   ->  move up
  • 0   ->  move to beginning of line
  • ^   ->  move to first non-blank character
  • $   ->  move to end of line
  • w  ->  jump to next word
  • b   ->  jump to previous word
  • gg ->  go to beggining of file
  • G  ->  go to end of the file
  • :n  ->  go to line number n
Editing and Deleting

  •  x    ->   delete a character
  •  dw  ->  delete word
  •  dd   ->  delete entire line
  •  D    ->  delete from cursor to end of the line
  •  u     ->  undo last action
  •  r     ->  replace single character
  •  yy   ->  copy entire line where the cursor is currently located
  •  p     ->  paste after cursor
  •  P     ->  paste before cursor

Search and Replace

  • /text  ->  search forward for text
  • ?text  ->  search backward for text
  •   n    ->  repeat search word forward
  •   N   ->  repeat search word backwards
  •  :%s/old/new/g  ->  replace all occurrences of old with new
  • :set nu  ->  show line numbers