Unix Notes
Unix Notes
Unix Notes
The UNIX file system allows the user to access other files not
belonging to them and without infringing on security. A file has a
number of attributes (properties) that are stored in the inode. In this
chapter, we discuss,
The file type and its permissions are associated with each file. Links
indicate the number of file names maintained by the system. This does
not mean that there are so many copies of the file. File is created by
the owner. Every user is attached to a group owner. File size in bytes is
displayed. Last modification time is the next field. If you change only
the permissions or ownership of the file, the modification time remains
unchanged. In the last field, it displays the file name.
For example,
$ ls l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 1 kumar metal 4174 may 10 15:01 chap02
-rw-rw-rw- 1 kumar metal 84 feb 12 12:30 dept.lst
1
-rw-r--r-- 1 kumar metal 9156 mar 12 1999 genie.sh
drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs
ls ld helpdir progs
drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs
File Ownership
When you create a file, you become its owner. Every owner is
attached to a group owner. Several users may belong to a single group,
but the privileges of the group are set by the owner of the file and not
by the group members. When the system administrator creates a user
account, he has to assign these parameters to the user:
The user-id (UID) both its name and numeric representation
The group-id (GID) both its name and numeric representation
File Permissions
For Example:
2
The first group has all three permissions. The file is readable,
writable and executable by the owner of the file. The second group has
a hyphen in the middle slot, which indicates the absence of write
permission by the group owner of the file. The third group has the write
and execute bits absent. This set of permissions is applicable to others.
Relative Permissions
3
chmod u+x xstart
The command assigns (+) execute (x) permission to the user (u), other
permissions remain unchanged.
Let initially,
Then, it becomes
Absolute Permissions
4
2 -w- write only
3 -wx write and execute
4 r-- read only
5 r-x read and execute
6 rw- read and write
7 rwx read, write and execute
will assign all permissions to the owner, read and write permissions for
the group and only execute permission to the others.
777 signify all permissions for all categories, but still we can
prevent a file from being deleted. 000 signifies absence of all
permissions for all categories, but still we can delete a file. It is the
directory permissions that determine whether a file can be deleted or
not. Only owner can change the file permissions. User can not change
other users files permissions. But the system administrator can do
anything.
----------
5
This is simply useless but still the user can delete this file
On the other hand,
-rwxrwxrwx
The UNIX system by default, never allows this situation as you can
never have a secure system. Hence, directory permissions also play a
very vital role here
This makes all the files and subdirectories found in the shell_scripts
directory, executable by all users. When you know the shell meta
characters well, you will appreciate that the * doesnt match filenames
beginning with a dot. The dot is generally a safer but note that both
commands change the permissions of directories also.
Directory Permissions
rwxr-xr-x (755)
Example:
mkdir c_progs
ls ld c_progs
6
Changing File Ownership
Usually, on BSD and AT&T systems, there are two commands meant
to change the ownership of a file or directory. Let kumar be the owner
and metal be the group owner. If sharma copies a file of kumar, then
sharma will become its owner and he can manipulate the attributes
chown
ls -l note
Once ownership of the file has been given away to sharma, the
user file permissions that previously applied to Kumar now apply to
sharma. Thus, Kumar can no longer edit note since there is no write
privilege for group and others. He can not get back the ownership
either. But he can copy the file to his own directory, in which case he
becomes the owner of the copy.
chgrp
ls l dept.lst
7
In this chapter we considered two important file attributes
permissions and ownership. After we complete the first round of
discussions related to files, we will take up the other file attributes.
8
FILTERS USING REGULAR EXPRESSIONS grep and sed
We often need to search a file for a pattern, either to see the lines containing (or
not containing) it or to have it replaced with something else. This chapter discusses two
important filters that are specially suited for these tasks grep and sed. grep takes care of
all search requirements we may have. sed goes further and can even manipulate the
individual characters in a line. In fact sed can de several things, some of then quite well.
It scans the file / input for a pattern and displays lines containing the pattern, the
line numbers or filenames where the pattern occurs. Its a command from a special family
in UNIX for handling search requirements.
will display lines containing sales from the file emp.lst. Patterns with and without quotes
is possible. Its generally safe to quote the pattern. Quote is mandatory when pattern
involves more than one word. It returns the prompt in case the pattern cant be located.
When grep is used with multiple filenames, it displays the filenames along with the
output.
grep options
grep is one of the most important UNIX commands, and we must know the
options that POSIX requires grep to support. Linux supports all of these options.
9
grep -i agarwal emp.lst
all the above three patterns are stored in a separate file pattern.lst
It is tedious to specify each pattern separately with the -e option. grep uses an
expression of a different type to match a group of similar patterns. If an expression uses
meta characters, it is termed a regular expression. Some of the characters used by regular
expression are also meaningful to the shell.
The basic regular expression character subset uses an elaborate meta character set,
overshadowing the shells wild-cards, and can perform amazing matches.
10
The character class
grep supports basic regular expressions (BRE) by default and extended regular
expressions (ERE) with the E option. A regular expression allows a group of characters
enclosed within a pair of [ ], in which the match is performed for a single character in the
group.
A single pattern has matched two similar strings. The pattern [a-zA-Z0-9] matches a
single alphanumeric character. When we use range, make sure that the character on the
left of the hyphen has a lower ASCII value than the one on the right. Negating a class (^)
(caret) can be used to negate the character class. When the character class begins with
this character, all characters other than the ones grouped in the class are matched.
The *
The asterisk refers to the immediately preceding character. * indicates zero or more
occurrences of the previous character.
Notice that we dont require to use e option three times to get the same output!!!!!
The dot
A dot matches a single character. The shell uses ? Character to indicate that.
Most of the regular expression characters are used for matching patterns, but there
are two that can match a pattern at the beginning or end of a line. Anchoring a pattern is
often necessary when it can occur in more than one place in a line, and we are interested
in its occurance only at a particular location.
grep ^2 emp.lst
11
Selects lines where emp_id starting with 2
grep 7$ emp.lst
It is possible that some of these special characters actually exist as part of the text.
Sometimes, we need to escape these characters. For example, when looking for a pattern
g*, we have to use \
To look for [, we use \[
To look for .*, we use \.\*
If current version of grep doesnt support ERE, then use egrep but without the E
option. -E option treats pattern as an ERE.
# ?include +<stdio.h>
12
grep E sengupta|dasgupta emp.lst
sed is a multipurpose tool which combines the work of several filters. sed uses
instructions to act on text. An instruction combines an address for selecting lines, with
an action to be taken on them.
sed supports only the BRE set. Address specifies either one line number to select a single
line or a set of two lines, to select a group of contiguous lines. action specifies print,
insert, delete, substitute the text.
Line Addressing
sed 3q emp.lst
Just similar to head n 3 emp.lst. Selects first three lines and quits
p prints selected lines as well as all lines. To suppress this behavior, we use n whenever
we use p command
sed n $p emp.lst
sed n 1,2p
7,9p
$p emp.lst
13
Selecting multiple groups of lines
There is adequate scope of using the e and f options whenever sed is used with
multiple instructions.
Let us consider,
cat instr.fil
1,2p
7,9p
$p
-f option to direct the sed to take its instructions from the file
Context Addressing
14
Context addresses also uses regular expressions.
Sed n /sa[kx]s*ena/p
/gupta/p emp.lst
Line addressing also is possible. Saves first 500 lines in foo1 and the rest in foo2
Text Editing
sed supports inserting (i), appending (a), changing (c) and deleting (d) commands
for the text.
$ sed 1i\
> #include <stdio.h>\
> #include <unistd.h>
> foo.c > $$
15
Will add two include lines in the beginning of foo.c file. Sed identifies the line without
the \ as the last line of input. Redirected to $$ temporary file. This technique has to be
followed when using the a and c commands also. To insert a blank line after each line of
the file is printed (double spacing text), we have,
sed a\
emp.lst
Selects all lines except those containing director, and saves them in olist
Substitution (s)
Substitution is the most important feature of sed, and this is one job that sed does
exceedingly well.
[address]s/expression1/expression2/flags
Only the first instance of | in a line has been replaced. We need to use the g
(global) flag to replace all the pipes.
16
sed also uses regular expressions for patterns to be substituted. To replace all occurrence
of agarwal, aggarwal and agrawal with simply Agarwal, we have,
We can also use ^ and $ with the same meaning. To add 2 prefix to all emp-ids,
sed s/<I>/<EM>/g
s/<B>/<STRONG>/g
s/<U>/<EM>/g form.html
An instruction processes the output of the previous instruction, as sed is a stream editor
and works on data stream
sed s/<I>/<EM>/g
s/<EM>/<STRONG>/g form.html
2233|a.k.shukla|g.m|sales|12/12/52|6000
9876|jai sharma|director|production|12/03/50|7000
5678|sumit chakrobarty|dgm|mrking|19/04/43|6000
Consider the below three lines which does the same job
17
sed s/director/member/ emp.lst
The // representing an empty regular expression is interpreted to mean that the search and
substituted patterns are the same
The interval RE - { }
sed and grep uses IRE that uses an integer to specify the number of characters preceding
a pattern. The IRE uses an escaped pair of curly braces and takes three forms:
The value of m and n can't exceed 255. Let teledir.txt maintains landline and mobile
phone numbers. To select only mobile numbers, use IRE to indicate that a numerical can
occur 10 times.
18
Line length between 101 and 150
You have to identify the segments of a line that you wish to extract and enclose each
segment with a matched pair of escaped parenthesis. If we need to extract a number, \([0-
9]*\). If we need to extract non alphabetic characters,
\([^a-zA-Z]*\)
Every grouped pattern automatically acquires the numeric label n, where n signifies the
nth group from the left.
To get surname first followed by a , and then the name and rest of the line. sed does not
use compulsorily a / to delimit patterns for substitution. We can use only any character
provided it doesnt occur in the entire command line. Choosing a different delimiter has
allowed us to get away without escaping the / which actually occurs in the pattern.
Source: Sumitabha Das, UNIX Concepts and Applications, 4th edition, Tata
McGraw Hill, 2006
19
MORE FILE ATTRIBUTES
Apart from permissions and ownership, a UNIX file has several other attributes,
and in this chapter, we look at most of the remaining ones. A file also has properties
related to its time stamps and links. It is important to know how these attributes are
interpreted when applied to a directory or a device.
This chapter also introduces the concepts of file system. It also looks at the inode,
the lookup table that contained almost all file attributes. Though a detailed treatment of
the file systems is taken up later, knowledge of its basics is essential to our understanding
of the significance of some of the file attributes. Basic file attributes has helped us to
know about - ls l to display file attributes (properties), listing of a specific directory,
ownership and group ownership and different file permissions. ls l provides attributes
like permissions, links, owner, group owner, size, date and the file name.
The hard disk is split into distinct partitions, with a separate file system in each
partition. Every file system has a directory structure headed by root.
All attributes of a file except its name and contents are available in a table inode
(index node), accessed by the inode number. The inode contains the following attributes
of a file:
File type
File permissions
Number of links
The UID of the owner
The GID of the group owner
File size in bytes
Date and time of last modification
Date and time of last access
Date and time of last change of the inode
An array of pointers that keep track of all disk blocks used by the file
Please note that, neither the name of the file nor the inode number is stored in the inode.
To know inode number of a file:
ls -il tulec05
Where, 9059 is the inode number and no other file can have the same inode number in the
same file system.
20
Hard Links
The link count is displayed in the second column of the listing. This count is normally 1,
but the following files have two links,
All attributes seem to be identical, but the files could still be copies. Its the link count
that seems to suggest that the files are linked to each other. But this can only be
confirmed by using the i option to ls.
A file is linked with the ln command which takes two filenames as arguments (cp
command). The command can create both a hard link and a soft link and has syntax
similar to the one used by cp. The following command links emp.lst with employee:
ln emp.lst employee
The i option to ls shows that they have the same inode number, meaning that
they are actually one end the same file:
The link count, which is normally one for unlinked files, is shown to be two. You
can increase the number of links by adding the third file name emp.dat as:
You can link multiple files, but then the destination filename must be a directory. A file is
considered to be completely removed from the file system when its link count drops to
zero. ln returns an error when the destination file exists. Use the f option to force the
removal of the existing link before creation of the new one
21
Where to use Hard Links
It creates link in directory input_files. With this link available, your existing
programs will continue to find foo.txt in the input_files directory. It is more convenient to
do this that modifies all programs to point to the new path. Links provide some protection
against accidental deletion, especially when they exist in different directories. Because of
links, we dont need to maintain two programs as two separate disk files if there is very
little difference between them. A files name is available to a C program and to a shell
script. A single file with two links can have its program logic make it behave in two
different ways depending on the name by which it is called.
We cant have two linked filenames in two file systems and we cant link a
directory even within the same file system. This can be solved by using symbolic links
(soft links).
Symbolic Links
Unlike the hard linked, a symbolic link doesnt have the files contents, but simply
provides the pathname of the file that actually has the contents.
ln -s note note.sym
Where, l indicate symbolic link file category. -> indicates note.sym contains the
pathname for the filename note. Size of symbolic link is only 4 bytes; it is the length of
the pathname of note.
Its important that this time we indeed have two files, and they are not identical.
Removing note.sym wont affect us much because we can easily recreate the link. But if
we remove note, we would lose the file containing the data. In that case, note.sym would
point to a nonexistent file and become a dangling symbolic link.
Symbolic links can also be used with relative pathnames. Unlike hard links, they
can also span multiple file systems and also link directories. If you have to link all
filenames in a directory to another directory, it makes sense to simply link the directories.
Like other files, a symbolic link has a separate directory entry with its own inode number.
This means that rm can remove a symbolic link even if its points to a directory.
A symbolic link has an inode number separate from the file that it points to. In
most cases, the pathname is stored in the symbolic link and occupies space on disk.
22
However, Linux uses a fast symbolic link which stores the pathname in the inode itself
provided it doesnt exceed 60 characters.
The Directory
A directory has its own permissions, owners and links. The significance of the file
attributes change a great deal when applied to a directory. For example, the size of a
directory is in no way related to the size of files that exists in the directory, but rather to
the number of files housed by it. The higher the number of files, the larger the directory
size. Permission acquires a different meaning when the term is applied to a directory.
ls -l -d progs
The default permissions are different from those of ordinary files. The user has all
permissions, and group and others have read and execute permissions only. The
permissions of a directory also impact the security of its files. To understand how that can
happen, we must know what permissions for a directory really mean.
Read permission
Read permission for a directory means that the list of filenames stored in that
directory is accessible. Since ls reads the directory to display filenames, if a directorys
read permission is removed, ls wont work. Consider removing the read permission first
from the directory progs,
ls -ld progs
Write permission
We cant write to a directory file. Only the kernel can do that. If that were
possible, any user could destroy the integrity of the file system. Write permission for a
directory implies that you are permitted to create or remove files in it. To try that out,
restore the read permission and remove the write permission from the directory before
you try to copy a file to it.
23
cp emp.lst progs
The write permission for a directory determines whether we can create or remove
files in it because these actions modify the directory
Whether we can modify a file depends on whether the file itself has write
permission. Changing a file doesn't modify its directory entry
Execute permission
cd progs
When we create files and directories, the permissions assigned to them depend on
the systems default setting. The UNIX system has the following default permissions for
all files and directories.
The default is transformed by subtracting the user mask from it to remove one or
more permissions. We can evaluate the current value of the mask by using umask without
arguments,
$ umask
022
This becomes 644 (666-022) for ordinary files and 755 (777-022) for directories umask
000. This indicates, we are not subtracting anything and the default permissions will
remain unchanged. Note that, changing system wide default permission settings is
possible using chmod but not by umask
24
MODIFICATION AND ACCESS TIMES
A UNIX file has three time stamps associated with it. Among them, two are:
Time of last file modification ls -l
Time of last access ls lu
The access time is displayed when ls -l is combined with the -u option. Knowledge of
files modification and access times is extremely important for the system administrator.
Many of the tools used by them look at these time stamps to decide whether a particular
file will participate in a backup or not.
Then, both times are set to the current time and creates the file, if it doesnt exist.
touch command (without options but with expression) can be used. The expression
consists of MMDDhhmm (month, day, hour and minute).
ls -lu emp.lst
It is possible to change the two times individually. The m and a options change the
modification and access times, respectively:
25
-rw-r--r-- 1 kumar metal 870 jan 26 16:50 emp.lst
It recursively examines a directory tree to look for files matching some criteria,
and then takes some action on the selected files. It has a difficult command line, and if
you have ever wondered why UNIX is hated by many, then you should look up the
cryptic find documentation. How ever, find is easily tamed if you break up its arguments
into three components:
The path_list comprises one or more subdirectories separated by white space. There can
also be a host of selection_criteria that you use to match a file, and multiple actions to
dispose of the file. This makes the command difficult to use initially, but it is a program
that every user must master since it lets him make file selection under practically any
condition.
Source: Sumitabha Das, UNIX Concepts and Applications, 4th edition, Tata
McGraw Hill, 2006
26
SIMPLE FILTERS
Filters are the commands which accept data from standard input manipulate it and
write the results to standard output. Filters are the central tools of the UNIX tool kit, and
each filter performs a simple function. Some commands use delimiter, pipe (|) or colon
(:). Many filters work well with delimited fields, and some simply wont work without
them. The piping mechanism allows the standard output of one filter serve as standard
input of another. The filters can read data from standard input when used without a
filename as argument, and from the file otherwise
Several UNIX commands are provided for text editing and shell programming.
(emp.lst) - each line of this file has six fields separated by five delimiters. The details of
an employee are stored in one single line. This text file designed in fixed format and
containing a personnel database. There are 15 lines, where each field is separated by the
delimiter |.
$ cat emp.lst
pr : paginating files
We know that,
cat dept.lst
01|accounts|6213
02|progs|5423
03|marketing|6521
04|personnel|2365
27
05|production|9876
06|sales|1006
pr command adds suitable headers, footers and formatted text. pr adds five lines of
margin at the top and bottom. The header shows the date and time of last modification of
the file along with the filename and page number.
pr dept.lst
01:accounts:6213
02:progs:5423
03:marketing:6521
04:personnel:2365
05:production:9876
06:sales:1006
blank lines
pr options
pr +10 chap01
pr -l 54 chap01
The command displays the top of the file. It displays the first 10 lines of the file,
when used without an option.
head emp.lst
28
-n to specify a line count
head -n 3 emp.lst
This command displays the end of the file. It displays the last 10 lines of the file,
when used without an option.
tail emp.lst
tail -n 3 emp.lst
displays the last three lines of the file. We can also address lines from the
beginning of the file instead of the end. The +count option allows to do that, where count
represents the line number from where the selection should begin.
Use tail f when we are running a program that continuously writes to a file, and we want
to see how the file is growing. We have to terminate this command with the interrupt key.
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).
The expression 55- indicates column number 55 to end of line. Similarly, -3 is the same
as 1-3.
Most files dont contain fixed length lines, so we have to cut fields rather than columns
(cutting fields).
29
-d for the field delimiter
-f for the field list
will display the second and third columns of shortlist and saves the output in
cutlist1. here | is escaped to prevent it as pipeline character
When we cut with cut, it can be pasted back with the paste command, vertically rather
than horizontally. We can view two files side by side by pasting them. In the previous
topic, cut was used to create the two files cutlist1 and cutlist2 containing two cut-out
portions of the same file.
Where each field will be separated by the delimiter |. Even though paste uses at least two
files for concatenating lines, the data for one file can be supplied through the standard
input.
Let us consider that the file address book contains the details of three persons
cat addressbook
Sorting is the ordering of data in ascending or descending sequence. The sort command
orders a file and by default, the entire line is sorted
sort shortlist
30
This default sorting sequence can be altered by using certain options. We can also sort
one or more keys (fileds) or use a different ordering rule.
sort options
sort t| k 2 shortlist
sort t| r k 2 shortlist or
sort t| k 2r shortlist
we can also specify a character position with in a field to be the beginning of sort
as shown above (sorting on columns).
sort n numfile
when sort acts on numericals, strange things can happen. When we sort a file
containing only numbers, we get a curious result. This can be overridden by n (numeric)
option.
31
Removing repeated lines can be possible using u option as shown above. If we
cut out the designation filed from emp.lst, we can pipe it to sort to find out the unique
designations that occur in the file.
sort c shortlist
sort t | c k 2 shortlist
When we concatenate or merge files, we will face the problem of duplicate entries
creeping in. we saw how sort removes them with the u option. UNIX offers a special
tool to handle these lines the uniq command. Consider a sorted dept.lst that includes
repeated lines:
cat dept.lst
uniq dept.lst
simply fetches one copy of each line and writes it to the standard output. Since uniq
requires a sorted file as input, the general procedure is to sort a file and pipe its output to
uniq. The following pipeline also produces the same output, except that the output is
saved in a file:
32
Counting frequency of occurrence (-c)
It takes input only from standard input, it doesnt take a filename as argument. By
default, it translates each character in expression1 to its mapped counterpart in
expression2. The first character in the first expression is replaced with the first character
in the second expression, and similarly for the other characters.
exp1=|/ ; exp2=~-
Changing case of text is possible from lower to upper for first three lines of the file.
tr cd |/ < emp.lst
Source: Sumitabha Das, UNIX Concepts and Applications, 4th edition, Tata
McGraw Hill, 2006
33
The vi Editor
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.
vi Basics
vi <filename>
In all probability, the file doesnt exist, and vi presents you a full screen with the
filename shown at the bottom with the qualifier. 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. This is the 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
For, text editing, vi uses 24 out of 25 lines that are normally available in the
terminal. To enter text, you must switch to the input mode. First press the key i, and you
are in this mode 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.
After the command is run, you are back to the default command mode. If a word has been
misspelled, use ctrl-w to erase the entire word.
Now press esc key to revert to command mode. Press it again and you will hear a
beep. A beep in vi indicates that a key has been pressed unnecessarily. 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 (the last line mode).
Invoke the execute mode from the command mode by entering a: which shows up in the
last line.
34
To clear the screen in command mode, press
ctrl-l
:set showmode
Messages like INSERT MODE, REPLACE MODE, CHANGE MODE, etc will appear in
the last line.
Pressing i changes the mode from command to input mode. To append text to the right
of the cursor position, we use a, text. I and A behave same as i and a, but at line extremes
I inserts text at the beginning of line. A appends text at end of line. o opens a new line
below the current line
COMMAND FUNCTION
i inserts text
a appends text
I inserts at beginning of line
A appends text at end of line
o opens line below
O opens line above
r replaces a single character
s replaces with a text
S replaces entire line
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:
35
Command Action
:W saves file and remains in editing mode
:x saves and quits editing mode
:wq saves and quits editing mode
:w <filename> save as
:w! <filename> save as, but overwrites existing file
:q quits editing mode
:q! quits editing mode by rejecting changes made
:sh escapes to UNIX shell
:recover recovers file from a crash
Navigation
A command mode command doesnt show up on screen but simply performs a function.
To move the cursor in four directions,
k moves cursor up
j moves cursor down
h moves cursor left
l moves cursor right
Word Navigation
Moving by one character is not always enough. You will often need to move faster
along a line. vi understands a word as a navigation unit which can be defined in two
ways, depending on the key pressed. If your cursor is a number of words away from your
desired position, you can use the word-navigation commands to go there directly. There
are three basic commands:
Example,
0 or |
36
$ moves to the end of the current line
The use of these commands along with b, e, and w is allowed
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
Absolute Movement
The editor displays the total number of lines in the last line
Editing Text
The editing facilitates in vi are very elaborate and invoke the use of operators. They use
operators, such as,
d delete
y yank (copy)
Deleting Text
Moving Text
37
p and P place text on right and left only when you delete parts of lines. But the same keys
get associated with below and above when you delete complete lines
Copying Text
Joining Lines
vim (LINUX) lets you undo and redo multiple editing instructions. u behaves
differently here; repeated use of this key progressively undoes your previous actions. You
could even have the original file in front of you. Further 10u reverses your last 10 editing
actions. The function of U remains the same.
You may overshoot the desired mark when you keep u pressed, in which case use
ctrl-r to redo your undone actions. Further, undoing with 10u can be completely reversed
with 10ctrl-r. The undoing limit is set by the execute mode command: set undolevels=n,
where n is set to 1000 by default.
The . (dot) command is used for repeating the last instruction in both editing and
command mode commands
For example:
2dd deletes 2 lines from current line and to repeat this operation, type. (dot)
/ search forward
? search backward
/printf
The search begins forward to position the cursor on the first instance of the word
38
?pattern
Searches backward for the most previous instance of the pattern
Command Function
We can perform search and replace in execute mode using :s. Its syntax is,
:address/source_pattern/target_pattern/flags
Interactive substitution: sometimes you may like to selectively replace a string. In that
case, add the c parameter as the flag at the end:
: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.
The ex mode is also used for substitution. Both search and replace operations also
use regular expressions for matching multiple patterns.
39
The features of vi editor that have been highlighted so far are good enough for a
beginner who should not proceed any further before mastering most of them. There are
many more functions that make vi a very powerful editor. Can you copy three words or
even the entire file using simple keystrokes? Can you copy or move multiple sections of
text from one file to another in a single file switch? How do you compile your C and Java
programs without leaving the editor? vi can do all this.
Source: Sumitabha Das, UNIX Concepts and Applications, 4th edition, Tata
McGraw Hill, 2006
40