220 likes | 361 Views
Introduction to UNIX. C. Vi Editor. Overview of vi. Performance Objectives 1. 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)
E N D
Introduction to UNIX C. Vi Editor Vi Editor
Overview of vi Performance Objectives 1. 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) Vi Editor
UNIX Family of editors • ex line editor • ed subset of ex • vi screen editor Vi Editor
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. Vi Editor
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. Vi Editor
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. Vi Editor
Vi Modes UNIX Shell vi filename ZZ or :wq Command Mode i a o <ESC> Input Mode Vi Editor
vi Window Display Line one Line two Line three ~ ~ ~ ~ ~ ~ Line n Command line File text Null lines EX cmd line Vi Editor
Vi Window Positioning vi window ^f Display Next segment File Vi Editor
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 Vi Editor
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 Perm File Temp File :w Vi Editor
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) Vi Editor
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 Vi Editor
Search For a Pattern • Search forward to pattern / ... / • Search backward to pattern ? ... ? • Advance to next pattern n (forward or reverse) Vi Editor
Replace A Pattern • Replace string with another :s/.../.../ (only on same line) • Replace string (global) :1,$s/.../.../g Vi Editor
Moving Text • ma mark the starting position with a • d'a delete text into a buffer • p put buffer contents after cursor ma Area to be moved d’a p Insert location Vi Editor
Copying Text • ma mark the starting position with a • y'a yank text into a buffer • p put buffer contents after cursor ma Area to be copied y’a p Insert location Vi Editor
Writing to a file • write current file :w • write named file :wfilename • overwrite named file :w! • write lines m - n to named file m,nw file Vi Editor
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 Vi Editor
Tailoring the Environment • For current session :setoptions<cr> • Example: :set nu <cr> will set line numbers • To view possible settings: :set all noautoindent nonumber noslowopen autoprint nonovice tabstop=8 noautowrite nooptimize taglength=0 nobeautify paragraphs= tags=tags /usr/lib/tags directory=/tmp prompt term=vt100 noedcompatible noreadonly noterse noerrorbells redraw timeout flash remap ttytype=vt100 hardtabs=8 report=5 warn noignorecase scroll=11 window=23 nolisp sections= wrapscan nolist shell=/bin/csh wrapmargin=0 magic shiftwidth=8 nowriteany mesg noshowmatch nomodelines noshowmode Vi Editor
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. Vi Editor
End of Module Complete VI Editor Exercises Vi Editor