A Simple Cookie Example
Vi Cheat Sheet
The following chart is a quick guide to the vi visual
text editor on the UNIX system. Commands that begin with :
are ex command and must be entered with a carriage return.
Normal vi commands are executed as soon as they are typed.
All input mode commands described below are terminated by
the ESCAPE key (which is CTRL/3 here at De Anza) except for
the r command which does not leave command mode to enter
input mode.
The reader will note the term [range] in the text sub-
stitution commands. Ranges are normally specified as i,j
where i and j will be line numbers. There are certain
``magic'' line number symbols such as . (stands for current
line), and $ (stands for last line). You may also use rela-
tive addressing such as .+5 (5 lines past current line).
See the text book for even more elaborate examples of line
number ranges.
_____________________________________________________________________________
CURSOR MOVEMENT
^ Move cursor to beginning of line.
$ Move cursor to end of line.
Arrow keys Move cursor one space in direction indicated by arrow.
control-N Move cursor to next line. Same as up arrow.
control-P Move cursor to previous line. Same as down arrow.
space bar Move the cursor one character to the right. Same as
forward arrow.
control-H Move cursor one character to the left. Same as backward arrow.
h,j,k,l Same as back arrow, down arrow, up arrow,
and forward arrow.
RETURN key Move cursor to beginning of next line.
- Move cursor to beginning of previous line.
H The HOME key. Move cursor to the top of the screen.
M The MIDDLE key. Move cursor to the middle of the screen.
L The LAST key. Move cursor to the last line of the screen.
w Move cursor forward to beginning of next word.
e Move cursor forward to the end of this word.
b Move cursor back to the beginning of this word.
control-D Scroll down a half window.
control-U Scroll up a half window.
control-F Scroll down a whole window.
control-B Scroll up a whole window.
nG Move cursor to line n. If n omitted, go to end of file.
n Go forward n lines. N must be an integer.
n- Go backward n lines.
RETURN key Go forward 1 line.
- Go backward one line.
______________________________________________________________________________
___________________________________________________________________________
TEXT INPUT MODES
i Enter input mode. Put new text before character cursor is on.
I Enter input mode. Put new text at beginning of current line.
a Enter input mode. Put text after current character.
A Enter input mode. Put new text after last character of current line.
o Open a blank line below the current line.
O Open a blank line above the current line.
r Overwrite current character with character following r.
R Overwrite starting at current character until ESCAPE is hit.
___________________________________________________________________________
________________________________________________________
STRING SEARCH COMMANDS
/string Move forward to first occurrence of string.
?string Move backward to first occurrence of string.
n Repeat search. This is a literal ``n''!!
N Repeat search, change direction.
/ Repeat forward search.
? Repeat backward search.
________________________________________________________
_______________________________________________________________________
ERASING AND DELETING
nx or nX Delete next/previous n characters. N is an integer.
D Delete from cursor to end of line.
ndd Delete n lines. N is an integer and n=1 if omitted.
J Join current line to line below. (Erases carriage return
separator).
u Undoes ANY vi command but is particularly useful to undo a
bad delete!!
dG Delete from current line to end of file.
d$ Delete from cursor to end of line (same as D).
d/text ESCAPE Deletes up to ``text''.
d) Deletes from cursor to end of sentence.
_______________________________________________________________________
_______________________________________________________________
CUT AND PASTE
mx Mark position. ``x'' will be some lowercase letter.
:'x,.d Delete from marked position to current line.
p or P Place text in the deleted text buffer after/before
the current line.
_______________________________________________________________
_______________________________________________________________________
TEXT SUBSTITUTION
:[range]s/string1/string2/ Change first occurrence of string1
to string2 over the range specified.
If range omitted, it is assumed to be
current line.
:[range]s/string1/string2/g Same as above but for EVERY occurrence
of string1 on the lines in the range.
________________________________________________________________________
______________________________________________________________________________
FILE-RELATED COMMANDS
:w file Write buffer to specified file. You supply name of file.
:w! file Same but overwrites file if it exists.
:i,jw file Write lines i through j into file. i <= j
:r file Read file in after current line.
:vi file Discard current buffer and go to another file.
This command is good if you want to cut and paste between
files.
ZZ Exit vi and save work in file specified on the vi command line.
:q! Exit vi without saving the current buffer into a file.
______________________________________________________________________________
Examples of Substitutions
:1,20s/America/United States/
Explanation: Substitute "United States" for "America" in the first 20 lines.
The will only substitute the first occurrence (if any) on each line.
****************************************************************************
:1,20s/America/United States/g
Explanation: Same substitution but for EVERY occurrence on lines 1 through
20. The "g" at the end means "global".
****************************************************************************
:.,40s/America/United States/
Explanation: Same substitution but from the current line (.) through line 40.
*****************************************************************************
:25,$s/America/United States/
Explanation: Do the substitution for the first occurrence of "America" from
line 25 through the last line ($) in the file.
****************************************************************************
:25,$s/^America/United States/
Explanation: Do the substitution only if "America" occurs at the beginning
of the line. The symbol ^ means beginning of line.
****************************************************************************
:1,$s/America$/United States/
Explanation: Substitutes for instances of "America" which lie at the ends of
lines. The $ symbol means end-of-line when inside the //.
****************************************************************************
:/government/s/America/United States/
Explanation: Make the substitution on the first line containing government,
starting the search at the current line.
****************************************************************************
:.+3,$-4s/America/United States/
Explanation: Make the substitution on lines containing "America" starting
from three lines past the current line to 4 lines before the end of the file.
****************************************************************************
:1,$s/X-[0-9]/X-14/
Explanation: Substitute X-14 for all lines containing X-1, X-2, etc. through
X-9. One character inside [] is matched.
****************************************************************************
:/local/,/national/s/America/United States/
Explanation: Make the substitute for lines which lie between the first line
containing "local" and the first line containing "national" starting the
search at the current line.
****************************************************************************
:g/local/,/national/s/America/United States/
Explanation: Make the substitution for all lines in the file containing
"America" whenever these lines are between lines containing "local" and
lines containing "national". The "g" is the global command which operates
on the whole file.
You can do global substitutions, deletions etc. when you want to operate on
lines between lines which contain certain strings.
EXAMPLES OF VI SEARCH PATTERN NOTATIONS
NOTE: In this document, a space character will be represented by an @ sign.
Example: /^[^a-zA-Z]
Explanation: Finds the first line which doesn't start with a letter.
Example: /one\>
Explanation: Finds the first line with a word ending with "one".
Example: /^@@*
Explanation: Finds the first line starting with one or more blanks.
Example: /.....
Explanation: Finds the first line containing AT LEAST 5 characters.
Example: /^[a-zA-Z].*[0-9]$
Explanation: Finds the first line which starts with a letter and ends with a
number.
Example: /^$
Explanation: Finds the first line which is completely blank (i.e. contains
no characters of any kind -- even spaces).
Example: /^\<Speak\>
Explanation: Finds the first line starting with the word "Speak".
Example: /\.$
Explanation: Finds the first line ending with a literal period.
Example: /.
Explanation: Finds the next line which is NOT completely blank.
Example: /[!.?]$
Explanation: Finds the next line ending with end-of-sentence punctuation.
Example: /[!.?].*[!.?]
Explanation: Finds the next line containing at least 2 ends of sentences.
Example: ?@$
Explanation: Finds the next line GOING BACKWARDS which has a space character
at the end of the line.
------------------------------------------------------------------------------
SUMMARY OF REGULAR EXPRESSION CHARACTERS
NOTATION MEANING
-------- -------
\( and \) Open (close) tagged field
\< and \> Beginning (end) of word
. Matches any character
[any-chars] Match any characters inside the []
^ and $ Match beginning (ending) of line
* Match zero or more repetitions of prev. character group
(char-sequence) Character sequence in () is treated as group. Used with *
\ Take next character literally i.e. not as reg. expression
[^any-chars] Match any characters NOT inside the []
MORE VI SUBSTITUTION EXAMPLES
Note: In this document, tabs will be denoted by -> and spaces will be
denoted by an @ sign. At the terminal you would actually hit the
tab key or space bar.
Example: :.-3,.+3s/^Yellow/Lemon yellow/
Explanation: Substitute "Lemon yellow" for all instances of "Yellow" that
lie at the beginning of lines from 3 lines before the current
line through three lines after the current line.
****************************************************************************
Example: :%s/^[@->]*//
Explanation: Make all indented lines (i.e. lines with whitespace at their
beginning) become non-indented lines. Note that the replace-
ment pattern contains NO CHARACTERS AT ALL.
****************************************************************************
Example: :%s/\//#/g
Explanation: Make all literal slash characters (/) in the file become pound
signs (#). Remember, the "g" at the end means that lines with
multiple slashes will have ALL slashes converted to pound signs,
not just the first occurrence on the line.
****************************************************************************
Example: :%s/\<the\>/a/
Explanation: For all lines in the file which contain the WORD "the" (not
just the character sequence "the"), change the first occurrence
of "the" on those lines to "a".
****************************************************************************
Example: :%s/^\([A-Za-z]\)/@@@@\1/
Explanation: Indent all lines which begin with a letter by four spaces.
Remember that the @ sign in this document represents a space.
****************************************************************************
Example: :s/\.$/!/
Explanation: Substitute an exclamation mark for the period at the end of the
current line. Note that a substitute with NO LINE NUMBER RANGE
will only look at the CURRENT LINE.
****************************************************************************
Example: :%s/11*/1/g
Explanation: If any line has a pattern with more than 1 consecutive 1's
(i.e. 1, 11, 111, 1111, etc.), replace the pattern with only
one 1. The "g" at the end of the line means this substitution
will occurrence for ALL instances of this pattern on any line.
****************************************************************************
Example: :%s/^\([@->]*\)SUMMARY\([@->]*\)$/\1ABSTRACT\2/
Explanation: Look for any line whose entire contents are just "SUMMARY"
preceded or followed by whitespace. Change to "ABSTRACT"
while retaining whitespace.
****************************************************************************
Example: :%s/^\([@->]\)/\1@@/
Explanation: Indent all indented lines by two more spaces.