menu
 
home home home books palm tech web

TN101 - vi Editor

101.1   Summary
101.2   Other Resources
101.3   vi Quick Reference
101.4   ex Quick Reference
101.5   Sample .exrc

101.1 Summary

vi is an important editor to know in the UNIX environment because it is always available. The following notes are reminders. For detailed information please see the following books:  
 

Learning the vi editor - A good tutorial for beginners.

The Ultimate Guide to vi and ex Text Editors - The best reference I have found on VI and EX.

101.2 Other Resources

UnixWorld vi tutorial - On line tutorial.

An Introduction to Display Editing with Vi - Written by Bill Joy, developer of vi.

Vi Editor - On line reference manual.

Vi macros - The details of macro writing.

Basic Vi FAQ

Advanced Vi FAQ

vim - improved vi for UNIX, NT and other operating systems. (free)

Slickedit - A GUI editor that supports vi commands. (commercial product)

101.3 vi Quick Reference

Editor Commands

[esc]Return to Command mode
. Repeat Previous Command
[ctrl]L Redraw screen
:q!Quit and abandon changes
ZZQuit and save changes
:wSave buffer without quitting vi
:nedit next file
:wfnWrite buffer to a new file
:w>>fnAppend buffer to a file
:n,nwfnWrite lines n to n to a new file
:rfnRead filename and insert
vi -rRecover file after a system crash

Insert/Change Commands

iBefore cursor
IBefore first nonblank char on line
aAfter cursor
AAt end of line
oOpen a line next line down
OOpen a line next line up
rxReplace single character with x
RReplace characters
cnChange characters
sDelete char and continue typing
SnDelete line and continue typing

Cursor movement commands

hLeft one character
jDown to same position in line below
kUp to same position in line above
lRight one character
wForward to first letter of next word
WSame as w, but only whitespace is separator
bBack to first letter of previous word
BSame as b, but only whitespace is separator
fxMove forward to character x in line, stop on char (repeat ;)
FxMove backward to character x in line, stop on char (repeat ;)

 

[space]Right one character position
0Beginning of current line
$End of current line
^First non-blank char of line
[return]Forward to beginning of next line
+First character of next line
-First character of previous line
(Back to beginning of current sentence
)Ahead to beginning of next sentence
{Back to beginning of current ¶
}Ahead to beginning of next ¶
txMove forward to character x in line, stop after char (repeat ;)
TxMove backward to character x in line, stop after char (repeat ;)

File Position Commands

HLeft end of top line on screen
nHn lines from top
MLeft end of middle line on screen
LLeft end of lowest line on screen
nLn lines from bottom
GLast line in work buffer
nGMove to line number n
[ctrl]DDown half screen
[ctrl]UUp half screen
[ctrl]FDown almost a full screen

 

[ctrl]BUp almost a full screen
[ctrl]EScroll down one line at a time
[ctrl]YScroll up one line at a time
z RtnMove current line to top of screen
zMove current line to center of screen
z-Move current line to bottom of screen
/patFind pat (n repeats N reverses)
?patPrevious line matching pat

Delete Commands

xCharacter at cursor
XCharacter before cursor
ddDelete current line
nddDelete n lines
dwDelete to end of word
ndwDelete n words
dbDelete to beginning of word
ndbDelete n words
d0Delete to end of line
d$Delete to beginning of line
d)Delete to end of sentence
d(Delete to beginning of sentence
d}Delete to end of paragraph
d{Delete to beginning of paragraph
dGDelete to end of file
d1GDelete to beginning of file

Buffers and Marks

YYank to buffer
yyYank to buffer
mxMark position with letter x
`xGoto mark x
'xGoto first of line with mark x
Y'xYank from current line to mark x
"xYYank to buffer x
PPut from buffer
ppPut from buffer
"xPPut from buffer x
d'xDelete to first of line with mark x

Misc

.Repeat command
JJoin two lines
uUndo a command
UUndo an undo command
n>>Shift n lines to right
n<<Shift n lines to left
>'xShift current line to mark x to right
<'xShift current line to mark x to right
:lShow control characters on line
:veShow vi version/td>

Set Behavior

:set ai Turn on autoindent
:set noai Turn off autoindent
:set nu Turn on line numbers
:set nonu Turn off line numbers
:set sw=n Set shiftwidth = to n
:setshow settings

101.4 ex Quick Reference

The vi editor is actually an interactive version of ex, and all of the ex commands are available inside vi. Some ex commands helpful to apply global changes or write macros.
form of commands: line_address command parameters

Line Address

.current line in file
1first line in file
$last line in file
nnth line in file
n,nrange of lines n to n
.-nnth line before current line
.+nnth line after current line
%all lines in file (1,$)
'xline with marker x
.,'xcurrent line through line with marker x
/pat/first line in forward search matching pattern
?pat?first line in backward search matching pattern
.,/pat/current line through first line in forward search matching pattern

Basic Commands

. Repeat Previous Command
as/pat/pat/substitute second pattern for first on lines addressed by a
%s/pat/pat/substitute second pattern for first in whole doc
%s/pat/pat/csubstitute with confirm
%s/pat/pat/gsubstitute multiple times per line
g/pat/pPrint all lines with pattern on display
g/pat/dDelete all lines with pattern
g!/pat/dDelete all lines without pattern
g/pat/y xYank all lines with pattern to buffer x

Executables

%!exepipe all lines through executable exe
n,n!exepipe lines n-n through executable exe
%!expandexpand tabs to spaces
%!expand -a convert spaces to tabs
r !dateRead output of date command line

Misc

:ab cc xxx abbreviate cc to be expanded to xxx
:uab ccturn abbreviation off for cc
:mapshow mapped keys
:map c xxx map char c to xxx
:unmap unmap char c
:map! c xxx map char c to xxx (in insert mode)
:unmap! unmap char (in insert mode)
@xexec named buffer x in a shell
:listshow control character on current line

101.5 Sample .exrc

A file named .exrc located in the user's home directory will be executed when vi starts. This example creates the macros described below:

  map # :?^$?+1,/^$/-1s/^/#/^M/^$^M/.^M
map % :?^$?+1,/^$/-1s/^#//^M/^$^M/.^M
map * :?^$?+1,/^$/-1!hang^M/^$^M/.^M
map @ :r !date^MkJ
map [ :?^$?+1,/^$/-1!hang -w^M/^$^M/.^M
map ] :?^$?+1,/^$/-1!hang -wn^M/^$^M/.^M
map { :?^$?+1,/^$/-1!hang -bw^M/^$^M/.^M
map } :?^$?+1,/^$/-1!hang -bwn^M/^$^M/.^M
# will prepend a # to all lines in current group and advance to cursor to next group
% will remove a leading # from all lines in current group and advance to cursor to next group
* will align all lines in current group with the first line and advance cursor to next group
@ appends date to end of current line
[ will wrap all lines in current group, indent same as first line, and advance cursor to next group
] same as above except we reduce right margin by same as left
{ will wrap all lines in current group, indent same as first line, (except first word of first line which will remain outside to left of paragraph) and advance cursor to next group
} same as above except we reduce right margin by same as left
Notes:

How the map # works:

map # :?^$?+1,/^$/-1s/^/#/^M/^$^M/.^M
 
# will prepend a # to all lines in current group and advance to cursor to next group
mapmap command
#key to map
:: key to get to ex
?^$?+11st address search backwards for empty line go forward 1 line
,address separator
/^$/-12nd address search forward for empty line go backward 1 line
ssubstitute command
/^/find beginning of line
/#/add #
^Mneed CR to finish line
/^$^M/find next empty line
.^Mmove forward one line
 
[books]  [palm]  [tech]  [web]  [GPG keys] 
© Copyright 2000. All rights reserved.