Simple Text Editors


vi

  • Console
Save and quit
ZZ    Save and quit
:x Save and quit
:wq Save and quit

:q Quit
:q! Quit without saving

:w Save
:w <file-name> Save as <file-name>
Insert mode
a   After the current cursor position
A End of the current line
i Before the current cursor position
I Beginning of the current line

esc Switch back to command mode
Delete
x   Delete character at current cursor position
X   Delete character before the cursor

cc   Replace current line with a blank line
dG   Delete all lines from cursor to end of file
Traversing a file
:<n>  Go to line number n
G   last line in file
0   First character on the current line
^ First non-whitespace character on the current line

$ end of current line

w   next word
b   previous word
%   go to matching parenthesis, curly brace, or square bracket
Copy and paste
yy   (copy current line)
dd   (cut current line into unnamed buffer) (delete)

p   (paste after the cursor)
P   (paste before the cursor)
Search and replace
:/pattern       Search for the specified pattern
n   Search for the next occurrence of the pattern
N  Search for the previous occurrence of the pattern
:s/a/b/g        Replace every occurrence of "a" with "b" on the current line
:%s/a/b/g Replace every occurrence of "a" with "b" in the entire file

:s/a/b/c Confirm the substitution

:n1,n2s/a/b/g Run the substitution for lines n1 to n2

:hi clear search   Disable the search highlighting
Undo
u   Undo the last action
U   Undo recent changes to the current line
.   Redo the last change (period)
Misc
o     Add a blank line after the current line
O  Add a blank line before the current line
  • set command:
:set all              Show a list of "set" options

:set [no]list Show hidden characters
:set [no]number Show line numbers

:set [no]ignorecase Perform case insensitive searches (:set ic)
  • Can run multiple commands by separating them with a "|" character:
:s/a/b/g | s/c/d/g
  • When editing multiple files (vi file1 file2) (vi $(...)), use :next, :prev, :first, and :last to switch between the files