Friday, July 13, 2001

Vi Editor Quick Reference

vi editor quick reference

n = number
c = character
(cr) = Return
del = delete

Cursor Movement Through Text:

l = character right h = character left
j = down a line k = up a line
w = word right b = word left
0 = beginning of line $ = end of line
( = beginning of sentence ) = end of sentence
{ = beginning of paragraph } = end paragraph
[[ = beginning of section ]] = end section
G = end of file nG = move to line n
- = beginning of next line up + or cr = start, next line down

Cursor Movement Through File:

nH = to top line down offset
nL = to bottom line up offset
M = to middle line
CTRL d = scroll down
CTRL u = scroll up
CTRL f = scroll forward 1 screen
CTRL b = scroll back 1 screen
CTRL e = scroll forward 1 line
CTRL y = scroll back 1 line
CTRL E = scroll forward,leave cursor
CTRL Y = scroll back,leave cursor

Text Operations:

d( = delete, cursor to start(sentence)
d) = delete, cursor to end(sentence)
x or dSPACE = delete a character
db = delete previous word
dw = delete a word
d0 = delete, cursor to start(line)
dd = delete a line
D = delete, cursor to end(line)
nJ = join n lines
i(cr) or a(cr) = split a line
. = repeat last command
u = undo last command
U = undo all commands to this line

Text Block Moving in Command Mode:

Marking and Returning:
mx = mark current line (x=some letter a-t)
`x = return to position prior to present position
'x = return to space of line marked x
" = return to position prior to commands. /,?, or G
d`x or d"x = del from here to mark x

Placing in buffer (unnamed & named)
y = yank from here to endpoint, if # given include lines
nyw = yank n words
nyy or nY = yank n lines
"anY = yank n lines, place in named buffer a, (a thru t)

Retrieving from buffer
p = put buffer contents after/below cursor
P = put buffer contents before/above cursor
"ap = put buffer a 's contents after cursor (a -t)
"np = recover the last 1-9 deletions of text
p = recover last deleted text, place after cursor
P = recover last deleted text, place before cursor

Buffer types
% = current buffer
# = alternate buffer
1 - 9 = buffers holding last 9 yanks/deletions
a - z = buffers holding explicit yanks/deletions
A - Z = buffers holding explicit yanks/deletions;
contents appended to
:m,nco# = copy lines m thru n after line #
:m,nm# = move lines m thru n after line #
>L = shift lines right 1 shiftwidth (23 line max,
cursor line and below)

cursor line and below)
>> = shift 1 line right 1 shiftwidth
#>> = shift # lines right 1 shiftwidth
<< = shift 1 line left 1 shiftwidth
#<< = shift # lines left 1 shiftwidth

Insert Mode or Text Mode from Command Mode:

r = replace a single character
R = replace; text overwrite from here to
i = insert before cursor
I = insert at front of line
a = append after cursor
A = append at end of line
o = open a line below
O = open a line above
s = substitute a character
S = substitute text with text
c = change from here to
cc= change # of lines
C = change from here to rest of line; if # given, then # following
lines also
CTRL t = shift right 1 shiftwidth (ai set)
CTRL d = shift left 1 shiftwidth (ai set)
CTRL v = quote(esc) next special character

Adding Text, Command Mode to Text Mode:

ncw = changes n words after cursor
C = change from cursor to end of line
ncc = changes n lines beginning with cursor linen

Search Commands from Command Mode:

CTRL g = find current line status
/word = find next occurrence of word, forward search
/ = find last requested searched for word
// or n = find next occurrence of the last searched for word
?word = find previous occurrence of word, backward search
N = search backwards for previous occurrence of last searched word
fc = find character c on current line
Fc = find previous c on current line
; = go to next/previous character on same line

Search patterns:

^ = first word of line $ = end of line
. = any one character .* = any characters
\< = beginning of word \> = end of word
\ = next char literally [a-z] = any character in range a-z
[str] = any chars in string [^str] = any char not in string

Global Substitution from Command Mode:

:g/s1/p = prints all lines with string "s1"
:g/^/s//string = prepend to each line
:g/$/s//string = append to each line
:g/s1/s//s2/ = sub 1st occurrence "s1" with "s2" on all lines
:g/s1/s//s2/g = sub all occurrences, all lines
:g/s1/s//s2/gc = " " " / " " , interactively
:g/\(ab\)\(cd\)/s//\2\1/g = swap patterns using numeric
position variable,all
:g/\(ab\)\(.\)/s//\2\1/g = swap patterns using numeric
position variable, all, without naming 2nd
variable ( . = any character)

File Manipulation from Command Mode:

!}fmt = format this paragraph
:w filename = writes contents to specified file
:w ! spell | fmt = To just see spelling errors:
:$r !spell % | fmt = To append spelling errors to buffer
:w !lpr = print the version currently in the edit buffer
:r filename = reads contents of filename into buffer after cursor
:r !cmd = read output of a command into buffer after cursor
:!cmd = execute ULTRIX commands in vi/ex mode
:!! = execute last shell cmd
:!lpr % = print it without leaving the editor
:pre } use this when OUT of file system space and can't write
:w /var/tmp} file normally, or look for owner id in
/usr/preserve or /var/tmp

To Exit:

Insert Mode to Command Mode: ESC
To save text and QUIT: ESC then ZZ
ESC then :wq (cr)
ESC then :x (cr)
To rename file: ESC then :f (cr)
To save text only: ESC then :w (cr)
To overwrite file ESC then :w! (cr)
To abort text: ESC then :q! (cr)
To edit next file in queue: ESC then ZZ then :n

File recovery after editor, system, or disk full crash, return to directory where file was opened and enter:

vi -r file

To start vi at line #x:

vi +x filename

To start vi at string, first occurrence:
vi +/string file

The following options can be setup in an .exrc file, or for each vi session (only):

noautoindent nonumber
autoprint open
noautowrite nooptimize
nobeautify paragraphs=IPLPPPQPP LIpplpipbp
directory=/tmp prompt
noedcompatible noreadonly
noerrorbells redraw
hardtabs=8 remap
ignorecase report=5
nolisp scroll=11
nolist sections=NHSHH HUnhsh
magic shell=/bin/csh
mesg shiftwidth=8
nomodeline noshowmatch
noslowopen tags=tags /usr/lib/tags
tabstop=8 taglength=0
term=vt100 noterse
ttytype=vt100 timeout
window=23 warn
wrapscan wrapmargin=1
nosourceany nowriteany