THE VI EDITOR

A combination of << and cat can be used to add lines to a file, and sed and file redirection can be used to modify the contents of a file. These tools are rough and awkward, and when it's time to either create new files or modify existing ones, a screen-oriented editor is needed. In UNIX, the screen editor of choice is called vi. vi is Visual Editor, in short.
There are a number of editors that may be included with the UNIX system, including ed, ex, vi, and EMACS. The latter two use the entire screen, a big advantage, and both are powerful editors.
The vi is a modal editor. A mode is like an environment. Different modes in vi interpret the same key differently. For example, if the user is in insert mode, pressing the A key adds an ‘a’ to the text, whereas in command mode, pressing the A key enters a, a single key abbreviation for the append command. Pressing Esc always returns the user to the command mode and if the system is already in command mode, it beeps to remind the user of that fact.
The second important characteristic of vi is that it's a screen-oriented program. It must know what kind of terminal, computer, or system that the user is using to work with UNIX. This probably won't be a problem because most systems are set up so that the default terminal type matches the terminal or communications program that the user is using.
The vi command by itself starts the editor, ready for the user to create a new file. The vi command with a filename starts vi with the specified file, so that the user can modify that file immediately.
There are primarily two types of editors available under UNIX namely,
 The ed line editor comparable to EDLIN IN DOS.
 The vi editor, a full screen editor.
CAUTIONS ABOUT VI
 The vi editor is very user unfriendly and offers no help facility.
 The same keystrokes can have more than one meaning depending on current mode and the key combinations are in no way mnemonic.
 The editor is fanatically case-sensitive
 The vi command does not lock a file while editing it. So it is possible that more than one user can edit it at the same time. The version of the file saved last is the one that is retained.
SALIENT FEATURES OF VI
 vi is omnipresent and is available in all UNIX systems.
 vi is fast.
 vi has got powerful UNDO features.
 vi has got a lot of support.
THE BASICS OF VI
vi and ex commands are compatible with each other but ex is a line editor only. All editing in vi is done in a buffer on a copy of the original until it is saved.
vi works in 3 different modes:
 Command mode
When in this mode, all keys are interpreted as commands.
 Insert mode
When in this mode, the keys are echoed in the edit buffer.
 ex Escape or Esc colon (:) mode
When in this mode, the keys are interpreted as commands and are echoed at the command line at the bottom of vi screen.
The bottom most line in vi is the command line where all commands in Esc: mode and messages are displayed. There are no error messages in vi - only beeps indicate the errors. Control or special keys cannot be used and the arrow keys cannot be used for cursor movement on most terminals.
IMPORTANT KEYS AND COMMANDS
KEY MEANING
The Esc key
Returns vi to the command mode and cancels partially formed commands.
The Return or Enter key Executes commands entered in ex Esc mode and starts a new line in Ins mode. In command mode, it simply takes the cursor to next line.
Forward Slash
“/” Specifies a string to be searched for in the existing file – the string appears in the status line following the forward slash “/” and is echoed at the beginning of the command line.
Question Mark
“?” Same as the forward slash key but the search is done backwards.
Colon
“:” To enter “ex Esc” mode - the command appears at the command line preceded by “:”.
Tilde
“~” Appears at the beginning of every line in vi.

INVOKING VI
COMMAND FUNCTION
vi Starts an empty edit buffer.
vi Edits the file or creates the file if it does not exist.
vi +3 Opens the file and goes to the third line.
vi + /bye Searches for the first occurrence of “bye” in the file .

CURSOR MOVEMENTS - NAVIGATING IN A FILE
The arrow keys should not be used to move, in vi though some terms do support them.









All vi commands involve the use of alphabetical keys, alone or in combination with Shift and Control Keys.
MORE CURSOR CONTROL IN VI
Commands marked with # can be invoked with a number preceding them indicating repeat count.

COMMAND FUNCTION
#w Moves forward by a word
#W Moves forward by a word till next space
#e Goes to the last character in the word
#E Same as #e but ignores punctuation
f[character] Upto specified character in current line – right
F[character] Same as above but - left
t[character] Same as f[c] but one character before
T[character] Same as F[c] but one character before
#b Go back by a word
#B Same as #b , but ignores punctuation
0 or ^ Goes to the beginning of a line
#$ Goes to the end of the line
#G Go to the line number # of the file
L Go to the last line
SCREEN COMMANDS
COMMAND FUNCTION
Ctrl-F Scrolls a screen forward - except last two lines
Ctrl-B Scrolls a screen backwards
Ctrl U Scrolls forward by half a screen
Ctrl-D Scrolls backwards by half a screen
Ctrl R or Ctrl L Re-draw and clear messages
Ctrl-G Status of edit

DELETING DATA
COMMAND FUNCTION
#dd Delete current line where cursor is present
#dw Delete words from the cursor position
#dW Same as #dw but includes punctuation
#x Delete character at the cursor position
#X Delete character before the cursor
d$ or D Delete current line from the cursor position to the end of the line
d0 Delete current line from the cursor position up to start of line

TEXT INSERTION
COMMAND FUNCTION
i Invokes insert mode and inserts before character
I Same as i but insertion at beginning of line
O Opens a blank line above current line
o Opens a blank line below the current line
a Same as i but inserts after cursor
A Same as a but appends at end of line

JOINING
The J command causes the lines of text below the current line to get joined with the current line.
LINE NUMBERING
In ex Esc mode the command: set nu sets line numbers and: set nonu removes them.
Block moves - Colon Commands
Copy 2nd line after 5th :2co5
Multiple lines copy :1,3co$
$ for last line, 1,3 is range
Move to line 6 :6
Delete three consecutive lines now (#dd) :3dd
Yet another way -
Symbol Means
“.” Current line where cursor is
“+” Line after current line
“-” Line before current line

Delete 3 lines below current line including current line :.,+3d
, denotes range
Move line 1 to after line 3 : 1mo3
Lines 1,2 and 3 after line 5 : 1,3mo5

SAVING AND EXITING
COMMAND FUNCTION
“:q!” Quit and abandon changes
“:wq” Save and quit
“:x” Replace old copy with new and quit
“ZZ” Save and Quit (from command mode)

REPLACING EXISTING TEXT
COMMAND FUNCTION
“r” Replace a single c at current cursor position
“R” Invoke insert mode and go on replacing till Esc
“s” Replace a single c with text - insert mode - continues till Esc - $ appears after c to be replaced
“S” Replace current line with new text - blanks entire current line - invokes insertion mode
“cw” Replaces a word - $ at end of word - replacement continues after end of $ if word size greater than current word
“C” Same as cw but acts on rest of line from current cursor position
“cc” Same as C but entire line

CUT AND PASTE
COMMAND FUNCTION
“#yy” Yank (cut !) specified number of lines into yank buffer
“p” Paste from yank buffer below current line
“P” Same as p but above current line

9 LIVES!
vi offers undo of as many as 9 line deletions (each deletion may be a group of lines) apart from the usual undo “u”. This follows the LIFO (Last In First Out) rule –
“#p or “#P - # is 1 to 9
PATTERN SEARCHING
Basic commands are:
/g search for pattern “g” forwards
?g search for pattern “g” backwards
n repeat the last / or ? command
SUBSTITUTION
These are ‘:’ (colon) commands.
To replace “string” by “string1” on the current line -
:s/string/string1/ - replaces only first occurrence
:s/string/string1/g - replaces all occurrences
Substitution can occur on multiple lines if range is specified -
:1,9s/string/string1/g
For substituting throughout the file -
:1,$s/string/string1/g
OR
:g/string/s//string1/g - note the double slash.
MACROS
vi macros are created using the “abbr” command -
:abbr uo “UNIX operating system”
In insert mode press “u” “o” and press “Spc bar” or “Enter” - the macro string is inserted at current cursor position.
:abbr -lists the macros defined
Macros can be unabbreviated or deleted -
:una uo - deletes macro uo
MAPPING KEYS TO COMMANDS
The “map” command maps a user defined key to a specific function.
The cursor movement keys j, k, l, h may be mapped by default to their respective cursor movement keys.
To map the key “ctrl-X” to the “wq” command –
:map ^X :wq^M
- note that ^M signifies , in vi “^” is typed by typing “Ctrl-v”

 Note : All characters typed with “ctrl” is taken as upper-case
An “unmap” command is available to cancel assignments -
:unmap ^X
Mapping in insert mode done using the “map!” command -
:map! ^x Sam fox
- In insert mode “Ctrl-x” will insert “Sam fox in the editor
An insert mode map cannot be unmapped unlike abbr
:map displays only keys mapped to command mode functions
RUNNING SHELL COMMANDS FROM WITHIN
 “:sh” temporarily “shells out” to $ and “ctrl-d” returns the shell
 “: !“ “!” stands for run which will directly run

READING COMMAND OUTPUT FROM WITHIN
This is done using “:r!” -
:r! date - reads in output of “date” at current cursor position
:r! banner Hi there

EDITING MULTIPLE FILES SIMULTANEOUSLY
vi can take multiple file arguments -
$ vi file1 file2 ..... OR metacharacters -
$ vi f*
:args
[file1] file2 file3 - The bracketed file taken for display/edit
:n - Switches to next file in list
:args
file1 [file2] file3
:rew
:args
[file1] file2 file3 -i.e. “rew” rewinds to file1
:rew! - discards changes and coercively rewinds
:e file - edits file outside chosen list but :args will not show this file in list
READ AND WRITE ACROSS FILES
Write first five lines of text from file1 to file2-
:1,5 w! file2
Read same text from file2 to file1-
:1,5 r file2
THE “SET” OPTIONS
The “set” option helps customise the vi environment -
: set all - displays all “set” options available
The more common options are:
SET COMMAND DEFAULT DESCRIPTION
:set number,nu nonumber Line numbers
:set showmode noshowmode Display vi mode
:set autowrite,aw noaw Save on n, rew, !
:set autoindent,ai noai Indent as first line
:set beautify,bf nobeautify Control characters omitted
:set directory,dir dir-/tmp Sets temporary directory
:set errorbells,eb noeb No error beep
:set ignorecase,ic noic Uppercase as lowercase
:set magic magic Allow all metacharacters
:set mesg nomesg Inhibit Write messages
:set redraw noredraw Act as intelligent terminal
:set scroll scroll=half window No. of lines to scroll
:set shell sh=/bin/sh Set which shell
:set term value of TERM Set which terminal
:set terse noterse Error messages terse
:set warn warn Warn save on Shell Esc
:set window dependent window speed No of lines in text window
:set wrapscan,ws ws Wrap pattern on find
:set warpmargin,wm wm=0 Blank screen margin right
:set writeany, wa nowa Allow writes freely

The “.exrc” file
vi reads .exrc before loading and all options like map,abbr and set can be entered here so that they are permanent for that vi session -
$ vi .exrc
set nu
set showmode
abbr uo UNIX operating system
map ^k :r! date^M
:wq

No comments:

Post a Comment