June 1, 1999 Vi Editor 1
Introduction to UNIX
C. Vi Editor
June 1, 1999 Vi Editor 2
Overview of vi
Performance Objectives1. Start/End VI (vi, ESC, :wq)
2. Move the Cursor and Window (h,j,k,l, ^f, ^b)
3. Insert, Change, Delete Text (i, a, x, dd)
4. Search and Replace text (/.../, :n, $s/.../.../g)
5. Move/Copy Text (ma, d'a, p)
6. Write and Read Files (:w file, :r file)
7. Tailor the VI Environment (:set)
June 1, 1999 Vi Editor 3
UNIX Family of editors
• ex line editor
• ed subset of ex
• vi screen editor
June 1, 1999 Vi Editor 4
Why learn vi/ex?
• Available on most UNIX systems.
• Works with a variety of terminals.
• Needs no special keyboard definition files.
• Allows the use of ex commands from vi.
• Provides for a customized editing environment.
June 1, 1999 Vi Editor 5
Some Conventions
• vi commands do not require a <CR>.
• vi commands not echoed.
• ex commands begin with : / ?
• ex commands are echoed and ended with a <CR>
• The character ^ represents the <ctrl> key.
June 1, 1999 Vi Editor 6
Vi Modes:
• COMMAND mode The default mode on entering vi. Anything typed is interpreted as a command.
• INSERT mode Enter INSERT mode by typing one of several
commands. Anything typed is interpreted as data. Exit INSERT mode by typing the <ESC> key.
June 1, 1999 Vi Editor 7
Vi Modes
UNIX Shellvi filename
Command Mode
i a o
Input Mode
<ESC>
ZZ or :wq
June 1, 1999 Vi Editor 8
vi Window Display
Line one Line twoLine three~~~~~~ Line n
Command line
File text
Null lines
EX cmd line
June 1, 1999 Vi Editor 9
Vi Window Positioning
File
vi window
^f Display Next segment
June 1, 1999 Vi Editor 10
Basic Window Positioning
• Move window forward 32 lines ^f
• Move window backward 32 lines ^b
• Move window forward one line ^e• Move window to line n of file :n• Move window to end of file G
• Move cursor up/dn left/rt or h j k l
June 1, 1999 Vi Editor 11
Making Changes Permanent
• vi uses a temp file for changes:
• Write file and quit editor :wq
• Undo last command: u or U
• Recovery: vi -r filename
TempFile
PermFile
:w
June 1, 1999 Vi Editor 12
Insert/Delete Functions
• Del current line dd
• Del current char x
• Insert before current char i ...
• Insert after current char a ...
• Insert after current line o ... (opens file at a line)
June 1, 1999 Vi Editor 13
More Vi Functions
• Open several files vi file1 file2 ...• End editor :q (does not save changes)• Redraw screen ^l (L)
• Join this line with next J
June 1, 1999 Vi Editor 14
Search For a Pattern
• Search forward to pattern / ... /
• Search backward to pattern ? ... ?
• Advance to next pattern n (forward or reverse)
June 1, 1999 Vi Editor 15
Replace A Pattern
• Replace string with another :s/.../.../ (only on same line)
• Replace string (global) :1,$s/.../.../g
June 1, 1999 Vi Editor 16
Moving Text
• ma mark the starting position with a
• d'a delete text into a buffer
• p put buffer contents after cursor
Area to be movedma
d’a
Insert locationp
June 1, 1999 Vi Editor 17
Copying Text
• ma mark the starting position with a
• y'a yank text into a buffer
• p put buffer contents after cursor
Area to be copiedma
y’a
Insert locationp
June 1, 1999 Vi Editor 18
Writing to a file
• write current file :w
• write named file :w filename
• overwrite named file :w!
• write lines m - n to named file m,nw file
June 1, 1999 Vi Editor 19
Useful Functions
• Re-edit current file, discard changes :e
• Edit named file :e file
• Read in and insert file at cursor :r file
• Execute command and return to vi :!cmd
June 1, 1999 Vi Editor 20
Tailoring the Environment• For current session :set options<cr>• Example: :set nu <cr> will set line numbers• To view possible settings:
:set all noautoindent nonumber noslowopenautoprint nonovice tabstop=8noautowrite nooptimize taglength=0nobeautify paragraphs= tags=tags /usr/lib/tagsdirectory=/tmp prompt term=vt100noedcompatible noreadonly notersenoerrorbells redraw timeoutflash remap ttytype=vt100hardtabs=8 report=5 warnnoignorecase scroll=11 window=23nolisp sections= wrapscannolist shell=/bin/csh wrapmargin=0magic shiftwidth=8 nowriteanymesg noshowmatchnomodelines noshowmode
June 1, 1999 Vi Editor 21
Order of Evaluation
• On startup, vi sets options defined by EXINIT variable.
• Then options defined in ~/.exrc file.
• Settings in ~/.exrc take precedence.
• Setting in .exrc file in CWD take precedence over ~/.exrc file and EXINIT.
• Finally, :set options of current session take precedence during that session.
June 1, 1999 Vi Editor 22
End of Module
Complete VI Editor Exercises