Text Editors: vi commands and tips
vi is a family of screen-oriented text editors which share certain characteristics, such as methods of invocation from the operating system command interpreter, and characteristic user interface features. The portable subset of the behavior of vi programs, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.
vi operates in either insert mode (where typed text becomes part of the document) or normal mode (where keystrokes are interpreted as commands). Typing "i" while in normal mode switches the editor to insert mode. Typing "i" again at this point places an "i" in the document. From insert mode, pressing the escape key switches the editor back to normal mode.
vi basic commands
:set ic
ignore case differences when searching.
:set ai
set automatic indent.
:set sm
show matching ( or { with ) or } in insert mode.
:set nu
show line numbers.
down-arrow up-arrow
move down/up 1 line.
right-arrow left-arrow
move right/left 1 character column.
0 $
go to 1st/last column of current line.
return
go down to 1st printable character of next line.
nw nb
move right/left n words (1 word if n omitted).
ng
go to line n (end of file if n omitted).
ctrl-f ctrl-b
page forward/backward 1 screen.
ctrl-d ctrl-u
page forward/backward half a screen.
[[ ]]
go to beginning of current/next c function.
/expressionreturn
search forwards for expression.
?expressionreturn
search backwards for expression.
n n
repeat last / or ? command in same/reverse direction.
ytarget
copy (yank) text up to target to buffer.
y
copy current line to buffer.
itextesc
insert text before cursor.
otextesc
open new line below cursor and insert text.
r
replace character under cursor with next typed.
rtextesc
replace text.
backspace
in insert mode, delete character before cursor.
x x
delete character under/before cursor.
nx
delete n characters under and to right of cursor.
nx
delete n characters before cursor.
dd
delete current line.
ndd
delete n lines.
d
delete from cursor to end of line.
p p
put back yanked or deleted text below/above current line.
j
join current and next lines.
:m,n s/old/new/gc
global replace (g=every occurrence on line, c=prompt);
m=.
means from current position, n=$ means to eof.
u
undo last change.
:q
quit, provided no changes were made.
:q!
quit without saving.
:w
save (write) changes.
:m,n w file
save lines m through n (default=all) to file.
: x
save changes and quit.