Conventions
italics items to be replaced by your own
requirements
Entering/leaving vi
($ represents Solaris system prompt)
$ vi name open or create file name
$ vi +n name open file at line n
$ vi + name open file at end
$ vi -r list saved files
$ vi -r name recover file name
$ vi name1 name2 edit first file; edit the rest via :n
$ vi -t tag name open file name at tag
$ vi +/pat name open at first occurrence of pat
$ view name open file for read only
:w write changes to file
:w name write to file name
:w! name overwrite file name
:wq write changes and quit
:q quit
:q! quit without saving changes
ZZ exit from vi, saving changes
vi modes
Command normal and initial mode
(other modes return here when
terminated)
Insert entered by a i A I o O c C s S R
terminate with
Last line reading input for : / ? or !
terminate with or to
execute
Moving around within the file
move cursor:
h or to left
j or to next line
k or to previous line
l or to right
0 to beginning of line
$ to end of line
^ to first non-white on line
w right one word
W right one word past punctuation
b back one word
B back one word past punctuation
e to end of word
E to end of word past punctuation
H to top of screen
L to bottom of screen
M to middle of screen
nG go to line n
G go to last line
/pat go to next line matching pat
?pat go to previous line matching pat
n repeat last / or ?
N repeat last / or ? in reverse direction
/pat/ +n n’th line after pat
?pat? -n n’th line before pat
% find matching (){ or }
) move cursor to next sentence
} move cursor to next paragraph
( move cursor to last sentence
{ move cursor to last paragraph
]] move cursor to next section/function
[[ move cursor to last section/function
fx move to x forward in current line
Fx move to x backward in current line
tx move forward to character before x
Tx move back to character after x
; repeat last f F t T in same direction
, repeat last f F t T in reverse direction
n| move to column n in current line
+ move to next line, at first non-white
- move to previous line, at first non-white
Inserting text
a append text after cursor
A append text at end of line
i insert text before cursor
I insert text at beginning of line
o open line below, ready for text
O open line above, ready for text
Changing Text
cw change word (or part of word)
cc change line
C change part of line to right of cursor
s substitute string for character under cursor
S substitute entire line
rx replace character under cursor with x
r break line at cursor position
R overwrite text to right of cursor
J join line below to current line
xp transpose characters
~ change case of letter
u undo last change
U undo all changes to line
:u undo previous last-line command
. repeat last change
Corrections when adding text
\ escape and
^ kill autoindent on current line
0 kill all autoindentation
Deleting text
x delete character under cursor
5x . . . and 4 characters to the right
X delete character before cursor
6X . . . and 5 characters to the left
dw delete word (or part of word)
3dw . . . and 2 complete words to the right
dd delete line
4dd . . . and the 3 following lines
D delete part of line to right of cursor
:5,10 d delete lines 5–10, inclusive
dG delete from current line to end of file
Copying and moving text
yy yank or copy line
3yy . . . and the 2 following lines
dd delete line
p put line below current line
P put line above current line
yw yank or copy word (or part of word)
3yw . . . and 2 complete words to the right
dw delete word (or part of word)
4dw . . . and 3 complete words to the right
p put word after cursor
P put word before cursor
"xy yank item to buffer x
"xd delete into buffer x
"xp put from buffer x, after cursor
"xP put from buffer x, before cursor
"dp retrieve d’th last line
:1,2 co 3 copy lines 1–2 and put after line 3
:4,5 m 6 move lines 4–5 and put after line 6
Searching and replacing
/pat go to next line matching pat
?pat go to previous line matching pat
n repeat last / or ?
N repeat last / or ? in reverse direction
:3,7s/ab/cd/ search lines 3–7 and replace ab by cd
:.,$ search from current line to end of file
:% search the whole file
Pattern-matching characters
^ beginning of line
$ end of line
. any character
\< beginning of word
\> end of word
[str] any character in str
[^str] any character not in str
[x-y] any character between x and y
* any number of preceding pattern
File manipulation
:sh run shell, then exit to return to vi
:!cmd run cmd, then to return to vi
:n edit next file in argument list
:n args specify new argument list
:e name edit file name
:e! re-edit, discarding changes
:e + name edit name, starting at end
:e +n name edit name, starting at line n
:e # edit alternate file
:f or show current file and line
:r name insert file after cursor
:n r name insert file after line n
:ta tag look in tags file for entry under tag
Adjusting the screen
(dumb terminals only)
z redraw screen with current line at top
z- redraw screen with current line at bottom
z. redraw screen with current line at centre
/pat/z- redraw screen with pat line at bottom
zn. use n line window
Marking and returning
`` return to previous place after / ? G
´´ return after / ? G at first non-white
mx mark position with letter x
`x move cursor to mark x
´x move cursor to mark x at first non-white
Initializing options
EXINIT environment variable for setting options
.exrc startup file for vi; put options here
set x enable option
set nox disable option
set x=val set x to val
set show changed options
set all show all options
set x? show value of option x
Useful configuration options
full short meaning
autoindent ai supply indentation
autowrite aw writes before some commands
ignorecase ic in regular expression matching
list shows tabs as ^I,
end-of-line as $
magic enables metacharacters for
matching
number nu number lines
redraw simulate smart terminal
scroll sets number of lines for scrolling
shiftwidth sw sets number of spaces for
showmatch sm shows matching bracket or brace
slowopen slow prevents fast updating for screen
tabstop ts sets tab intervals
window specifies number of lines in
window
wrapscan ws search wraps round end of file
wrapmargin wm automatic line splitting
Specifying terminal type
(% and $ are system prompts)
% setenv TERM type csh and all Version 6
$ TERM=type; export TERM sh in Version 7
See also man tset
Some terminal types
2621 43 adm31 dw1 h19 vt100
2645 733 adm3a dw2 i100 sun
300s 745 c100 gt40 mime sun-cmd
33 act4 dm1520 gt42 owl
37 act5 dm2500 h1500 t1061
4014 adm3 dm3025 h1510 vt52
No comments:
Post a Comment