VI Editor Quick Reference

From Nearline Storage
Jump to navigation Jump to search

Starting Vi

  • edit an empty buffer: vi
  • edit a file: vi filename

Leaving Vi

  • write buffer to file: :w
  • write buffer to file filename: :w filename
  • quit: :q
  • quit discarding changes: :q!
  • write changes and quit: :wq
  • write changes and quit: ZZ

Special Notes

  • Use the escape key, <esc>, to leave insert mode or to cancel an incomplete command.
  • If a command doesn't work, try hitting <esc> and entering the command again.

Motion commands

  • move up one line: k
  • move down one line: j
  • move left one character: h
  • move right one character: l
  • scroll up one line: <ctrl>y
  • scroll down one line: <ctrl>e
  • scroll up (default is a half page): <ctrl>u
  • scroll down (default is a half page): <ctrl>d
  • show next page page: <ctrl>f
  • show previous page page: <ctrl>b
  • move to beginning of next line: +
  • move to beginning of previous line: -
  • move to beginning of first screen line: H
  • move to middle line of screen: M
  • move to last line of screen: L
  • move to the nth line: nG
  • move to the nth line: :n
  • move to the next occurrence of c on the current line: fc
  • move to the character in front of the next occurrence of c on the current line: tc
  • move forward to the next word: w
  • move forward to the end of the next word: e
  • move backward to the previous word: b

All commands except M, nG, and :n can be preceeded by an integer to indicate the distance to move.

Text entering commands

  • append text at end of line: A
  • append text after cursor: a
  • insert text at beginning of line: I
  • insert text before cursor: i
  • open a new line above cursor: O
  • open a new line below cursor: o

All commands must be terminated with an <esc> after the text has been entered.

Text deletion commands

  • delete character left of cursor: X
  • delete character under cursor: x
  • delete rest of word: dw
  • delete rest of sentence: d)
  • delete rest of paragraph: d}
  • delete to beginning of current line: d^
  • delete to end of current line: d$
  • delete to end of current line: D
  • delete line containing cursor: dd
  • delete the current line upto and including c: dfc
  • delete the current line upto c: dtc

All commands can be preceeded by an integer to indicate the number of characters, words, lines etc. to be deleted.

Text alteration commands

  • replace text: R
  • replace character with c: rc
  • transpose two characters: xp
  • join the next line to the end of the current line: J
  • undo last change: u
  • undo last change to current line: U
  • change rest of word: cw
  • change rest of sentence: c)
  • change rest of paragraph: c}

The commands R, cw, c) and c} must be terminated with an <esc> after the new text has been entered. All commands, except xp, u, and U can be preceded by an integer to indicate the amount of alteration to perform.

Text moving commands

  • yank a copy of the current line and place it in a buffer: yy
  • put the last item yanked or deleted before the cursor: P
  • put the last item yanked or deleted after the cursor: p
  • yank a copy of the current line and place it in buffer c: "cY
  • put contents of buffer c on a new line above the cursor: "cP
  • put contents of buffer c on a new line below the cursor: "cp

The commands Y and "cY can be preceeded by and integer to indicate the number of lines to copy.

Search Commands

  • search forward for pattern: :/pattern/
  • search forward for pattern: /pattern/
  • search backward for pattern: :?pattern?
  • search backward for pattern: ?pattern?
  • delete the next line containing pat1: :/pat1/d
  • substitute the next occurance of pat1 with pat2: :/pat1/s/pat1/pat2
  • repeat the last search: n
  • repeat the last search in opposite direction: N

Global parameter

  • substitute pat2 for pat1: :g/pat1/s/pat1/pat2
  • delete all occurrences of pat1: :g/pat1/d
  • print all occurrences of pat1: :g/pat1/p

Addresses

  • the nth line: n
  • lines n through k: n,k
  • the current line: .
  • the last line: $
  • n lines after the current line: +n
  • n lines before the current line: -n
  • the last n+1 lines: $-n,$

Miscellaneous

  • read file filename into the buffer after the current line: :r filename
  • read the output of command cmd into: the buffer after the current line: :r !cmd
  • write the buffer to file filename replacing its previous contents: :w! Filename
  • write the buffer to the end of file filename: :w >> filename
  • turn on line numbering: :set number
  • turn off line numbering: :set nonumber
  • turn on auto indenting: :set autoindent
  • turn on auto indenting: :set ai
  • turn off auto indenting: :set noautoindent
  • turn off auto indenting: :set noai