Logo vividSec
vi cheatsheet

vi cheatsheet

December 9, 2024
4 min read
Table of Contents

vi commands

  • i - insert before cursor
  • a - append after cursor
  • A - insert at start of line
  • I - append at end of line
  • O - open new line above cursor

replace commands

  • r - replace character under cursor (type new character)
  • R - enter replace mode until ESC is pressed
  • c <scope> - change text, specifying size of text to change; stays in insert mode until ESC is pressed.
  • cc - change complete line
  • cw - change to start of next word
  • c3l - change next three characters
  • c0 - change to start of line
  • c1G - change to start of file
  • cG - change to end of file
  • c$ - change to end of line
  • C - change to end of line

delete commands

  • x - delete character under cursor
  • X - delete character to left of cursor
  • d <scope> - delete to buffer, specifying size of text to change
  • dd - delete complete line
  • dw - delete to start of next word
  • d3l - delete next 3 characters
  • d0 - delete to start of line
  • d1G - delete to start of file
  • dG - delete to end of file
  • d$ - delete to end of line
  • D - delete to end of line

copy & paste (yank & put) commands

  • yy - yank complete line
  • yw - yank to start of next word
  • y0 - yank to start of line
  • yG - yank to end of file
  • p - paste buffer after current position
  • P - paste buffer before current position

undo commands

  • u - undo last edit (or insert)
  • U - restore the current line to original
  • m <letter> - set mark for current line
  • ‘ <letter> - go to mark
  • d <letter> - delete to mark
  • y’ <letter> - yank (copy) to mark
  • yyp - duplicate lines
  • xp - swap characters
  • J - join current and next line
  • . - repeat last edit (or insert)
  • ~ - invert letter case and move 1 character right

search and replace commands

  • /pattern - search forwards for pattern
  • ?pattern - search backwards for a pattern
  • n - repeat last search
  • N - repeat last search in opposite direction
  • :1,$s/old/new/g - replace all instance of old with new
  • :1,20s/old/new/gc - in the first 20 lines, replaces all old with new; ask for confirmation of each change
  • :n,ms/old/new/g - replace all instances of old with new between lines n and m

vi session options

  • aShowmode - show command/ insert mode
  • autoindent - autoindent on insert
  • number - number lines
  • nomagic - disable regular expressions

misc. commands

  • :r <file> - read file and place contents after current line (merge)
  • :w - write file
  • :w! - write file, overriding protection mode
  • W <file> - write to named file
  • :wq - write file and quit
  • :x - write file and quit
  • :q - quite without writing
  • :q! - force quit, even if file is not written
  • :w! - write file, overriding protection mode
  • :! <cmd> - execute shell command
  • :r!<cmd> - insert contents of shell command