Contact Session - 5
Course Name :
Systems Programming
BITS Pilani, Pilani Campus
IMP Note to Self
2
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
IMP Note to Students
⮚ It is important to know that just login to the session does not
guarantee the attendance.
⮚ Once you join the session, continue till the end to consider you
as present in the class.
⮚ IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
⮚ Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT
3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Agenda
⮚ The vi basics
⮚ Input mode – entering and replacing text
⮚ Saving text and quitting – the ex mode
⮚ Navigation
⮚ Editing text
⮚ Undoing last editing instructions
⮚ Repeating the last command
⮚ Searching for a pattern
⮚ Substitution – search and replace
⮚ cp, cut, paste commands.
4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
vi basics
• To write and edit some programs and scripts, we require
editors
• UNIX provides vi editor for BSD system – created by Bill
Joy
• Bram Moolenaar improved vi editor and called it as vim
(vi improved) on Linux OS
5
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
The vi Editor
The three modes of vi Editor:
1.Command Mode: The default mode of the editor where
every key pressed is interpreted as a command to run on
text. To copy, delete text & navigate, this mode is used.
2.Input Mode: Every key pressed after switching to this
mode actually show up as text. To input text, this mode is
used.
3.ex Mode(Last Line Mode): The mode used to handle
files (like saving) and perform substitution.
6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
The vi Editor
7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
INPUT MODE – ENTERING AND
REPLACING TEXT
8
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
INPUT MODE – ENTERING AND
REPLACING TEXT
Insertion of Text (i and a)
9
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
INPUT MODE – ENTERING AND REPLACING TEXT
Insertion of Text at Line Extremes (I and A)
10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
INPUT MODE – ENTERING AND REPLACING TEXT
Opening a New Line (o and O)
11
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
INPUT MODE – ENTERING AND REPLACING
TEXT
Replacing Text (r, s, R and S)
12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
INPUT MODE – ENTERING AND REPLACING
TEXT
Input Mode Commands
13
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Saving the Work (:w)
14
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Saving and Quitting (:x and :wq)
15
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Aborting Editing (:q)
16
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Writing Selected Lines
17
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Writing Selected Lines
18
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Escape to the UNIX Shell (:sh and [Ctrl-z])
19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Recovering from a Crash (:recover and –r)
20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING
Save and Exit Commands of the ex Mode
21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Movement in the Four Directions (h, j, k and l)
22
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Movement in the Four Directions (h, j, k and l)
23
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Word Navigation (b, e and w)
Example
5b takes the cursor 5 words back
3w takes the cursor 3 words forward
24
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Moving to Line Extremes (0, | and $)
25
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Moving to Line Extremes (0, | and $)
26
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Scrolling ([Ctrl-f], [Ctrl-b], [Ctrl-d] and [Ctrl-u])
27
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION
Absolute Movement (G)
28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Editing Text – Deleting characters (x, X)
29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Editing Text – Deleting lines (dd)
30
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Editing Text – Moving Text (p, P)
31
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Editing Text)
They use operators, such as,
d delete
y yank (copy)
dd delete entire line
yy copy entire line
6dd deletes the current line and five lines below
32
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Editing Text – Copying Text (y and p)
33
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Editing Text – Joining Lines (J)
34
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Undoing Last Editing Instructions (u and U)
35
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Repeating the Last Pattern Search (n and N)
36
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
SEARCHING FOR A PATTERN
• / search forward
• ? search backward
/printf The search begins forward to position the
cursor on the first instance of the word
?patternSearches backward for the most previous
instance of the pattern
n Repeats search in same direction of
original search
37
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT
Search and Repeat Commands
38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE
Substitution – Search and Replace ( :s )
39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE
Substitution – Search and Replace ( :s )
We can perform search and replace in ex mode using :s
Its syntax is,
:address/source_pattern/target_pattern/flags
:1,$s/director/member/g
:1,50s/unsigned//g
:3,10s/director/member/g
:.s/director/member/g
:$s/director/member/g
40
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE
Substitution – Search and Replace ( :s )
41
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE
Command Function
:nd Deletes nth line
:m,n d Deletes line from m to n
:n mo p Moves line n after line p
:m,n mo p Moves line m to n after line p
:m co p Copies line m after line p
:m,n co p copies line m to n after line p
:m,n w file1 writes line m to n to a file file1
:m,n w >>file1 Appends line m to n to file file1
:r file1 Reads the contents of the file file1 at
current cursor position
:r !command Executes shell command and the
output of the command is read at the
current cursor position.
42
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cp – COPYING A FILE
43
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cp – COPYING A FILE
44
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cp – COPYING A FILE
45
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File Management Commands
Activity Single Source Multiple Source
Copy file cp file1 file2 cp file1 file2 file3 destdir
Move file mv file1 file2 mv file1 file2 file3 destdir
Remove file rm file1 rm –f file1 file2 file3
create directory mkdir dir mkdir –p dir1/dir2/dir3
copy directory cp –r dir1 dir2 cp –r dir1 dir2 dir3 destdir
move directory mv dir1 dir2 mv dir1 dir2 dir3 destdir
remove directory rm –rf dir1 rm –rf dir1 dir2 dir3
Systems Programming - V 3.0
•4
BITS Pilani, Pilani Campus
cut Command
• It is used for slitting the file vertically
head -n 5 emp.lst | tee shortlist
will select the first five lines of emp.lst and saves it to
shortlist
• We can cut by using -c option with a list of column
numbers, delimited by a comma (cutting columns)
cut -c 6-22,24-32 shortlist
cut -c -3,6-22,28-34,55- shortlist
47
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cut Command
• Most files don’t contain fixed length lines, so we have
to cut fields rather than columns (cutting fields)
-d for the field delimiter
-f for the field list
cut -d \ | -f 2,3 shortlist | tee cutlist1
will display the second and third columns of shortlist
and saves the output in cutlist1. here | is escaped to
prevent it as pipeline character
48
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cut Command
• To print the remaining fields, we have
cut –d \ | -f 1,4- shortlist > cutlist2
49
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
paste Command
• When we cut with cut, it can be pasted back with the
paste command, vertically
paste cutlist1 cutlist2
We can view two files side by side
50
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
paste: pasting file (contd.,)
• We can specify one or more delimiters with -d
paste -d “|” cutlist1 cutlist2
Where each field will be separated by the delimiter |
51
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
paste Command
Joining lines (-s)
Let us consider that the file address book contains the
details of three persons
cat addressbook
paste -s addressbook -to print in one single line
paste -s -d ”| | \n” addressbook -are used in a circular
manner
52
Systems Programming - V 3.0 BITS Pilani, Pilani Campus
IMP Note to Self
53
Systems Programming - V 3.0 BITS Pilani, Pilani Campus