Upload
others
View
73
Download
1
Embed Size (px)
Citation preview
Module 3 Unix and Shell Programming
MODULE : 3
The vi editor. Basics. The .exrc file. Different ways of invoking and quitting vi. Different modes
of vi. Input mode commands. Command mode commands. The ex mode commands. Illustrative
examples Navigation commands. Repeat command. Pattern searching. The search and replace
command. The set, map and abbr commands. Simple examples using these commands.
The shells interpretive cycle. Wild cards and file name generation. Removing the special
meanings of wild cards. Three standard files and redirection. Connecting commands: Pipe.
Splitting the output: tee. Command substitution. Basic and Extended regular expressions. The
grep, egrep. Typical examples involving different regular expressions.
Topics from chapters 7, 8 and 13 of text book 1. Topics from chapter 2 and 9 ,10 of
text book 2
THE VI EDITOR
vi BASICS
Invoking vi editor
vi <filename>
In all probability, the file doesn’t exist, and vi presents you a full screen with the filename
shown at the bottom with the qualifier, [New file]. The cursor is positioned at the top and all remaining lines of the screen show a ~. They
are non-existent lines. The last line is reserved for commands that you can enter to act on text. This line is also
used by the system to display messages. When file is opened in a vi editor, by default it will be opening in a command mode.
This is the mode where you can pass commands to act on text, using most of the keys
of the keyboard. This is the default mode of the editor where every key pressed is interpreted as a
command to run on text. You will have to be in this mode to copy and delete text.
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 1
Module 3 Unix and Shell Programming
To enter text, you must switch to the input mode. First press the key i, and moved to
input mode and ready to input text. Subsequent key depressions will then show up on the
screen as text input. After text entry is complete, the cursor is positioned on the last character of the last line.
This is known as current line and the character where the cursor is stationed is the
current cursor position. This mode is used to handle files and perform substitution.
Sample vi editor file with some text enteredThis is the vi editor [Enter]It operates in three different modesThis is just a sample text. ~~~
Use [Esc] key to revert to command mode. Actually, the text entered has not been saved on disk but exists in some temporary storage
called a buffer. To save the entered text, you must switch to the execute mode Invoke the execute mode from the command mode by entering a : which shows up in The
last line.
THE THREE MODES OF vi EDITOR
Command Mode:
When user starts up vi editor, user are in “command mode”. The default mode of the
editor where every key pressed is interpreted as a command to run on text. This mode allows to copy, delete text (edit files) and to move to any other modes. When
keys are pressed in this mode it simply performs functions, it doesn’t displayed on the
screen. When you are in command mode, letters of the keyboard will be interpreted as
commands. Some of the command mode commands are : To delete a text command x is used, to
delete entire line use dd command, to put the character use P or p command, to yank the
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 2
Module 3 Unix and Shell Programming
text use y command, use of yy copies entire line, to undo the recent actions use u
command. When user in a command mode, the letters of the keyboard will be interpreted as
commands. These commands are one or two characters long, and can be entered with
few keystrokes. Once the editing is finished save the changes by moving to ex mode by using :w
command. Unnecessary pressing of [Esc] in this mode sounds a beep but also conforms you are in
this mode. As shown in the figure, from command mode user can get into the input mode by
giving any of these keys i a I A o O r R s S. The control can be brought back to the command mode by using the <Esc> key. From within the command mode the user can get into the ex mod by using the :
Input mode:
To enter the text, have to change from default command mode to Input mode Every key pressed after switching to this mode actually shows up as text. This mode is
invoked by pressing one of the keys among i a I A o O r R s S. In the above command list i and I stands for insertion, a and A stands for appending the
text, o and O stands for opening a new blank new line, r and R stands for replacement of
text, s and S stands for substitution of new text. This mode permits the insertion of new
text, appending the existing text and replacement, substitution of text. The i can be used to insert text anywhere in a line, I insert text only at the beginning of a
line. To append text to the right of the cursor position use a. To append text at the end of the
line use A. To open a line below the current line use o. To open a line above the current line use O. To replace one single character with another, user r followed by the character that
replaces the one under the cursor. To replace more than a single character use R followed by the text and then press [Esc]. To replace a single character with multi-character text, move the cursor to the point and
then press s. The S replaces the entire line irrespective of the cursor position. After pressing S, the
entire line vanishes from sight. Key in your text, and then press [Esc]. As shown in the figure, at any time user can go back to the command mode from insert
mode by pressing the <Esc> key.
Ex mode (Last line mode):
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 3
Module 3 Unix and Shell Programming
This mode is also known as the last-line mode and allows the user to use the command
in the bottom line in vi screen. This mode is used to handle files (like saving) and perform substitution. Pressing a : in the command mode invokes this mode and cursor moves to the bottom of
the screen. The bottom line of the vi screen is called the command line and it’s used to display the
entered ex mode commands. The commands entered in this mode are displayed in the
command line, the last line. Anything entered in front of the colon : prompt it is taken as
an ex command. Some of the sample ex mode commands are : :w to save the file contents, command
:wq used to save and quit the editor, :q to just quit the editor, :q! to quit the editor by
discarding the changes made After inserting ex mode command press [Enter], so it moved back to command mode.
After the command is run then it’s moved back to the default command mode.
Switching between the modes is depicted in the following diagram.
As shown in the figure; when user starts up vi editor, user is in “command mode”. From
command mode user can enter to input mode by pressing I,i,a,A,s,S,R,r,O,o commands.
From input mode user can move back to command mode by pressing [esc]. From
command mode to ex mode is invoked by pressing :. From ex mode pressing [enter]
move to the command mode.
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 4
Module 3 Unix and Shell Programming
*****************Review Questions/ University Questions*************************
Explain the different modes of vi editor with neat diagram.
Explain the three modes of vi and explain how you can switch from one mode to another.*******************************************************************************
The Repeat Factor
vi provides repeat factor in command and input mode commands. The repeat factor used as a
command prefix to repeat the command as many times as the prefix. Command mode command
k moves the cursor one line up. 10k moves cursor 10 lines up, here 10 is acting as a repeat factor.
So it helps to speed up the operation.
INPUT MODE – ENTERING AND REPLACING TEXT
To enter from command mode to input mode following commands are used:
Insert and Append (I,a,I and A) Replace (r, R, s and S) Open a line( o AND o)
Note: After the completion of text entry using any of these commands, have to return back to the
command mode by pressing [Esc].
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 5
Module 3 Unix and Shell Programming
Insertion of Text (i and a)
The simplest type of input is insertion of text.
i Existing text will be shifted right
Pressing i key changes the mode from command to input. Further key depression (press) will
result in text being entered and displayed on the screen.
If the i command is invoked with the cursor positioned on existing text, text on its right will be
shifted further without being overwritten.
The insertion of text with ‘i’ is shown in the fig. along with the position of the cursor.
The vi editor
i full-screen [Esc]
the vi full-screen editor
To append text to the right of the cursor position, use ‘a’
a Existing text will also be shifted right
followed by the text wish to key in. After finished editing, press [Esc].
the vi editor
a, is very useful [Esc]
the vi editor, is very useful
Insertion of Text at line Extremes (I and A)
I and A work at line extremes by performing necessary navigation to move there
I Inserts text at beginning of line
A Appends text at end of line
These two commands are very useful to enter comment lines in C programs
Use I on an existing line that need to convert to a comment, and then enter the symbol /* After
pressing [Esc], use A to append */ at the end of the line and press [Esc] again.
Using I and A to create a comment line in a C program.
The following code is used to check the palindrome numbers
I/* [Esc]
/* The following code is used to check the palindrome numbers
*/ [Esc]
/* The following code is used to check the palindrome numbers */
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 6
Module 3 Unix and Shell Programming
Opening a new line (o and O)
To open a line below from anywhere in a line, simply press ‘o’. This inserts an empty line below
the current line.
‘O’ (Uppercase O) opens a line above the current line. After completion of text entry press [Esc].
Vi and ex are one and the same editors
o It is due to William Joy[Esc]
Vi and ex are one and the same editors
It is due to William Joy
Replacing Text (r,s,R and S)
To change existing text, vi provides mainly four commands.
To replace a single character with another, use ‘r’
r Used to replace a single character
Vi and ex are one and the same editors
rX
Vi and eX are one and the same editor
So it replaces the text under the cursor by the new text entered.
When more than one character is need to be replaced with multiple characters ie (for example a
character b with csb, here one character need to replace with three.) use a ‘s’.
s Replaces one character with many
vi deletes the character under the cursor and switches to Input Mode. It may also show $ at that
location to indicate that replacement will not affect text on its right.
To replace multiple characters, use a repeat factor. 3s replaces three characters with new text.
Vi is a link of ex
s one[Esc]
vi is one link of ex
R and S command act similar to lowercase ‘r’ and‘s’ except that act on a larger group of
characters.
R Replaces all text on the right of the cursor position
S Replaces the entire line irrespective of cursor position (Existing line disappears)
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 7
Module 3 Unix and Shell Programming
vi is one link of ex, just an example text line
R simple[Esc]
vi is one link of ex, just an simple text line
Summary of Input mode commands
Command Functioni
I
Inserts text before the current character
Inserts text at the beginning of current linea
A
Appends text after the current character
Appends text at the end of the current lineo
O
Opens a line under the current line and inserts supplied text in it.
Opens a line above the current line and inserts supplied text in it.r
R
Replaces the current character at the cursor with the new one
Replaces a set of characters with a new set of characterss
S
Substitute the current character with the supplied text
Substitute the current line with the supplied text
OR
Command Function
i Insert text to the left of cursor (existing text shifted right)
aAppend text to right of cursor (existing text shifted right)
IInsert text at beginning of line (existing text shifted right)
AAppend text at the end of line
oOpens a line below
OOpens a line above
rch Replaces single character under cursor with ch
R Replaces text from cursor to right(existing text overwritten)
sReplaces single character under cursor with any number of character
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 8
Module 3 Unix and Shell Programming
SReplaces entire line
SAVING TEXT AND QUITTING – THE EX MODE
When you edit a file using vi, the original file is not distributed as such, but only a copy of it that
is placed in a buffer. From time to time, you should save your work by writing the buffer
contents to disk to keep the disk file current. When we talk of saving a file, we actually mean
saving this buffer. You may also need to quit vi after or without saving the buffer.
Some of the save and exit commands of the ex mode is:
Saving Your work (:w)
To save the buffer and remain in the editor use :w command. This command will help to
write the buffer to disk. Enter a :, which appears on the last line of the screen, then w and finally [Enter]: :w[Enter] “samplefile”, 9 lines, 321 characters With the :w command optionally it is possible to specify a filename as well. In that case,
the contents are separately written to another file.
Saving and Quitting (:x and :wq)
To save the contents and quit the editor (i.e., return to the shell), use the :x (exit)
command. : x [Enter] “samplefile”, 9 lines, 321 characters $_ To save and quit use :wq command. Note: It is also possible to save and quit the editor is by using ZZ, a command mode
command.
Aborting Editing( :q)
To abort the editing process and quit the editing mode without saving the buffer
use :q command.
:q[Enter] Won’t work if buffer is unsaved
$_
Vi also has a safety mechanism that prevents from aborting accidentally if the file is
modified. The following message is typical when try to do so:
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 9
Module 3 Unix and Shell Programming
No write since last change (:quit! Overrides) If the buffer has changed and want to abandon (reject) the changes, then use,
:q! Ignores all the changes made and quits
It returns to the prompt irrespective of the status of the buffer.
Writing selected lines
w can be prefixed by one or two addresses separated by a comma to save lines to a
different file. The command :10, 50w newpgm.c Writes 41 lines to newpgm.c file Here saves lines 10 through 50 to the file newpgm2.c. To save a single line, command is: :6w newpgm.c Writes 6th line to another file The symbols ‘.’ And ‘$’ have special significance- the (dot) represents the current line
and $ represents the last line of the file. It can be used singly or in combination. Example: :.w tempfile Saves current line(where cursor is positioned) :$w tempfile Saves last line :.,$w tempfile Saves current line through end
Escape to the UNIX shell (:sh and [Ctrl-z])
To obtain the shell prompt use ex mode command :sh command. This returns a shell prompt. It allows executing any UNIX commands and then returning
to the editor using [ctrl-d] or exit.
Recovering from a crash (: recover and –r)
The vi stores most of its buffer information in a hidden swap file. This file will remain on
disk. To save the file contents use either :recover or vi –r foo to recover as much of foo as
possible.
Save and Exit Commands of the ex Mode
Command Action:w Saves file and remains in editing mode:x Saves file and quits editing mode:wq Saves file and quits editing mode:w pgm.c Like save As , it saves the file contents to new file called pgm.c:w! pgm.c As above, but overwrites existing file.:q Quits editing mode when no changes are made to a file.
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 10
Module 3 Unix and Shell Programming
:q! Quits editing mode but after abandoning changes:n1,n2w module Writes lines n1 to n2 to file module:.w file.c Writes current line to the file build.sql:$w db.sql Writes last line to the file build.sql:!cmd Runs cmd command and returns to Command Mode:sh Escapes to UNIX shell:recover Recovers the file from a crash
NAVIGATION
Functions of different commands in command mode are described in this section. A command
mode command doesn’t show up on the screen but simply performs the function.
Movement in the Four Direction (h,j,k and l)
Vi provides the keys h,j,k and l to move the cursor in the four directions. These keys are placed
adjacent to one another in the middle row of the keyboard. Without a repeat factor, these keys
move the cursor by one position.
Use these keys for moving the cursor vertically:
k Moves the cursor UP
j Moves cursor DOWN
To move the cursor along a line, use these commands:
h Moves cursor LEFT
l Moves cursor RIGHT
The repeat factor can be used as a command prefix with all these four commands.
4k moves the cursor 4 lines up and 20h takes it 20 characters to the left.
Summary Table
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 11
Command Meaning Direction
k moves cursor up UP
j Moves cursor down DOWN
h Moves the cursor left LEFTl
Moves the cursor right RIGHT
Module 3 Unix and Shell Programming
Word Navigation (b,e and w)
There are 3 basic navigation commands:
b Moves back to beginning of word
e Moves forward to end of word
w Moves forward to beginning of word
A repeat factor speeds up cursor movement along a line.
Example:
5b takes the cursor five word back, while 3w takes the cursor three words forward. A word is
simply a string of alphanumeric characters and _.
The keys B,E and W perform functions similar to those of their lowercase counterparts except
that punctuation is skipped.
Moving to Line Extremes
Moving to the beginning or end of a line is a common requirement. It is achieved by keys 0,| and
$.
To move to the first character of a line use 0 or |
0 or | 30| moves cursor to column 30
The | takes a repeat factor and using that, it is possible to position the cursor on a certain column.
To position the cursor on column 30, use 30|.
$ moves to the end of the current line
$ Moved to the end of the current line
The use of these commands along with b, e, and w is allowed
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 12
Module 3 Unix and Shell Programming
Scrolling
Faster movement can be achieved by scrolling text in the window using the control keys. The
two commands for scrolling a page at a time are
ctrl-f scrolls forward
ctrl-b scrolls backward
10ctrl-f scroll 10 pages and navigate faster
ctrl-d scrolls half page forward
ctrl-u scrolls half page backward
5ctrl-d scrolls 5 pages forward.
Absolute Movement
To know the current line number can press [Ctrl-g]:
“/etc/passwd” [Read Only] line 45 of 125 ---45%---
The cursor is on line 45 (45% of 125), and this read only file has 125 lines in all.
Use G command with line number as repeat factor to locate the offending lines.
To move to the 40th line, use
40G Goes to line number 40
To move to the beginning of the file, use
1G Goes to line number 1
The end of the file is reached by simply using
G Goes to end of file
Summary Table
Command Function
bMoves back to the beginning of a word
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 13
Module 3 Unix and Shell Programming
e Moves forward to the end of a word
w Moves forward to the beginning of next word
0 (zero) Moving to the beginning of a line
| Moving to the certain column of a line
$ Moving to the end of a line
ctrl – f Scrolls one page forward = > 2 ctrl – f
ctrl – b Scrolls one page backward
ctrl - d Scrolls half page forward
ctrl – u Scrolls half page backward
20G Moves the cursor to the beginning of the line number 20
1G Moves the cursor to the beginning of the very first line
G Moves the cursor to the beginning of the last line
EDITING TEXT
Deleting text ( x and dd )
The simplest text deletion is achieved with the x command. This command deletes the character
under the cursor.
Move the cursor to the character that needs to be deleted and then press x,
The character under the cursor gets deleted the text on the right shifts left to fill up the space.
Repeat factor also applies here. 4x deletes the current character as well as three characters from
the right.
Deletion of the left character can be handle by the “ X ” command .
Entire lines can be deleted(removed) with “ dd “ command. Here also repeat factor applies.10dd
deletes the current line and 9 lines below it.
This is the vi full-screen editor from UCB
x
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 14
Module 3 Unix and Shell Programming
This is the vi fullscreen editor from UCB
Four spaces back
This is the vi full-screen editor from UCB
4x
This is the vi screen editor from UCB
This is the vi editor from UCB
It is superior to ed, and friendlier too
It is due to William Joy
It is slow in getting started but is quite powerful
dd
It is superior to ed, and friendlier too
It is due to William Joy
It is slow in getting started but is quite powerful
2dd
It is slow in getting started but is quite powerful
Moving text ( p and P )
Vi uses these 2 commands for all “ put “ operations that follow delete or copy operations.
p => it places the character right to the cursor . Puts the deleted or yanked text after the
current character.
P => it places the character left to the cursor. Puts the deleted or yanked text before the
current character.
Example : To correct sdtio.h to stdio.h, have to transpose the character d to t. Delete the d and
put it after the t. Move the cursor to the d in sdtio.h and use these two commands.
x sdtio.h becomes stio.h – cursor on t
p d put on right- stio.h becomes stdio.h
P places text on the left of the cursor.
To put entire line at different location, p places text below the current line and P places text
above.
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 15
Module 3 Unix and Shell Programming
Coping text(y,p)
Vi uses the term yanking for copying the text. In vi extracting a copy of the required text is
known as yanking.
y => it copies the single character
yy => it copies the entire line – copies the current line
10yy=> it copies the current line & below 9 line.
This copied text has to be placed at the new location. The put commands are the same – p and P.
Joining Lines (J)
To loin the current line and the line following it, use J. This command will join the 2 lines by
deleting the newline character which is present between the two lines
4J 4J joins following 3 lines with current one
Undoing last editing instructions
Last change can be made undo by pressing u. This will undo the most recent single editing
changes by restoring the position before the change. It undoes only the last edit, must use in
command mode
When a number changes have been made to a single line, vi allows user to discard all changes
before user move away from the current line. It undoes all the changes in the current line.
U don’t move away from current line.
The command reverses all changes made to the current line i.e all modifications that have been
made since the cursor was moved to this line.
Summary Table
Command Meaningp Puts the deleted(or yanked) text after the current characterP Puts the deleted(or yanked) text before the current characterx Deletes the current charactery Yanks a text object.X Deletes the character before the cursord Deletes a line or a range of linesu Undoes only the last edit
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 16
Module 3 Unix and Shell Programming
U Undoes all the changes on the current line
REPEATING THE LAST COMMAND (.)
The . (dot) command is used for repeating both Input and Command Mode commands that
perform editing task. ‘Use the actual command only once, and then repeat it at other places with
the . dot command.
Ex: Deleted two lines of text with 2dd, then to repeat this operation elsewhere, have to do is to
position the cursor at the desired location and press . , this will repeat the last editing instruction
performed.
Ex: Indent a group of lines use : i[Tab][esc], only once . Then you can move the cursor to each
line and simply press .
The . Command can be used to repeat only the most recent editing operation- insertion, deletion
or any action modifies buffer
Doesn't applicable to navigation, and searching options. Search commands (/ and ? ) cannot be
repeated using the (.) command as these commands do not make changes to the editor buffer.
SEARCHING FOR A PATTERN (/ AND ? )
Searching can be made in both forward and reverse directions and can be repeated. It is
initiated from the command mode by pressing /, which is shows up in the last line.
The format is as follows: /[pattern] [Enter]
Ex: /printf[Enter] Searches forward
The search begins forward to position the cursor on the first instance of the word (printf). Vi
searches entire file, so if the pattern can’t be located until the end of file , the search wraps
around to resume from the beginning of the file. i.e during the forward search after the end of
the file is reached, the search continues from the beginning of the file. If the search still fails, vi
responds with the message Pattern not found
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 17
Module 3 Unix and Shell Programming
? Pattern [Enter]
The sequence searches backward for the most previous instance of the pattern, the wraparound
features also applies here but in the reverse direction. i.e after the beginning of the file is
reached the search continues from the end of the file. Both of these command are command
mode commands.
Repeating the last pattern search ( n and N)
To repeat a search in the direction the previous search was made with /and ? use n.
n Repeats search in same direction of original search
n => the user can press “ n ” repeatedly to scan all instances of the string. Cursor is positioned at
the beginning of the pattern
N => it reverses the direction pursued by n , i.e. this is used to retrace the search path of n
Summary table
Command Function
/pat Searches forward for the pattern
?pat Searches backward for the pattern
n Repeat search in same direction along which previous search was made
N Repeat search in REVERSE direction along which previous search was made
SUBSTITUTION --- SEARCH & REPLACE (:S)
Vi offers another powerful feature that of substitution, which is achieved with the ex mode s
(substitution) command.
The syntax is as follows
:address/ source_pattern / target_pattern/flags
Here the source_pattern is replaced with target_pattern in all lines specified by address.
Address can be one or a pair of numbers. 1,$ -> addresses all lines in a file i.e. from 1st line to
last line. Common flag is “ g “ , which carries out the substitution for all occurrences of the
pattern in line.
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 18
Module 3 Unix and Shell Programming
Example: :1,$s/csa/csb/g Searches for csa and its substituted by csb in entire file
If we omit the “ g” flag , the substitution will be carried out only for the first occurrence in each
addressed lines.
The target pattern is optional. If it is not mentioned then it will delete all instances of the source
pattern in all lines matched by the address.
Example: 1,50 s/ note / /g deletes note everywhere in line 1 to 50
The range of lines that are to be affected by the substitution can also be specified.
:3,10 s / director / member / g Substitute line 3 through 10
: .s / director / member / g Only the current line
: $s / director / member / g Only the current line
: %s/printf/scanf/g Entire file printf is substituted by scanf
Interactive Substitution
To selectively replace a string, use confirmatory(c) parameter as a flag.
Example: :1,$s/director/member/gc
Each line is selected in turn, followed by a sequence of carets in the next line, just below the
pattern that requires substitution. The cursor is positioned at the end of this caret sequence,
waiting for your response. This sequence repeated for each of the matched lines. A y performs
the substitution, any other response doesn't
:1,$s/printf/scanf/gc
printf(“string is %s”, a);
When the above command is executed, the vi pauses at the replacement point and waits for the
user’s response. A yes(y or Y) make the replacement whereas no answer (n or N) doesn’t make
the replacement and the search continues.
THE FILE .EXRC
When we open a file using vi, we do some settings depending on our needs say, to set line
number, to set indentation, to set the tab space etc in the file. However, these settings or
customizations last only till the file is open. Some of these settings any user would always like to
retain on opening any file. These settings can be grouped in a file called .exrc file, and hence the
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 19
Module 3 Unix and Shell Programming
settings become permanent. Whenever a file is opened using vi, vi looks for the .exrc file in the
home directory of the user. If present, it reads the file and applies the settings in the file being
opened. And hence any customization we would like to have in the file should be put in this.
.exrc is a file located in $HOME/ .exrc, which holds many ex mode commands and a
series of set commands. Execution of these provides a suitable working environment with the vi.
The vi reads file $HOME/.exrc on startup. This file helps to create abbreviations, redefine the
keys to behave differently and also make variable settings.
Typical entries of a .exrc file is depicted as
$ cat.exrc
set number
ab p printf
ab s scanf
map x :wq^M
When file is opened in vi editor, the .exrc file in the home directory will be read and all the
commands present in it will be executed, thus building up the required editing environment.
THE SET, MAP AND ABBR COMMAND
The set command
The several options can be used by the user to customize the vi environment. For example, line
number can be made to appear automatically, the current mode of the vi editor can be made to be
displayed automatically, case sensitivity can be removed during pattern matching. For this an ex
mode command called the set command is used. By using set command it is possible to change
the look and feel of vi .
:set number command sets the line number option. When this option is set, line numbers appear
automatically. By default, no line number appears in vi editor. To remove the line number can
use : set nonumber
The command :set showmode is used to display the mode in which the editor is present
currently. Automatic displaying of the modes can be stopped by using noshowmode option along
with the set command i.e. :set noshowmode
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 20
Module 3 Unix and Shell Programming
The command :set autoindent automatically indents every new line keyed in by the user with
one or several tabs. Auto indenting can be cancelled by using the command :set noautoindent
Options that are set using set commands are applicable only to those sessions in which they are
given. If the user wants the settings to be applicable permanently, relevant settings must be set
using corresponding commands in the .exrc file.
Command Meaning
:set ic Ignores case when searching
:set ai Sets autoindent
:set noai To unset autoindent.
:set number Displays lines with line numbers on the left side.
:set ro Changes file type to "read only"
Map Command
Assign existing vi commands to a custom key or define own custom commands. Using this
command one can connect one or more commands to a single key. This process is known as
mapping. Such a facility allows the user to perform complex editing tasks with a single
keystroke.
For example: map Q :q!
This command maps the Q key with the quit operation. So, whenever the user wants to quit the
file, instead of typing ':q!', the user can simply type 'Q' from the escape mode.
The ab command
This is also an ex mode command and is used to have short hand abbreviations for commonly
used expressions or the input text.
For example while writing the C programs, the printf and scanf words can be abbreviated using
the single characters p and s respectively as shown below.
:ab p printf
:ab s scanf
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 21
Module 3 Unix and Shell Programming
When the abbreviated character p followed by a space character or the enter key is used the text
printf appears at the place of p.
So ab command helps to define abbreviations and that are expanded into a specific sequence of
characters when they are encountered while users are typing text in Insert mode. Frequently used
words can be abbreviated. For example, say, a user uses the word 'include' lot many times. The
user can create an abbreviation 'inc' for the word include.
ab inc include
Whenever user type 'inc' followed by a space in a file, it automatically gets converted to 'include'.
While abbreviations set during a session are valid only for that session If the user wants the
abbreviations to be applicable permanently then corresponding abbreviations entries must be
made in .exrc file.
********************Review Questions: University Questions ******************
Explain what the following commands do i) :.,10w foo ii) :$w! foo. In
which mode are the commands executed.
How do commands u and U differ? When will U fail to work?
Explain the ex and input mode vi editor commands with example
With example explain the different navigation commands
Illustrate how to search and replace a pattern with example
Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 22