You are on page 1of 14

UNIX: vi Editor

vi

vi editing a temporary, empty file. Tildes signify lines not present in the
file.

Developer(s)

Bill Joy

Written in

C

Operating system

Unix-like

Type

text editor

License

BSD License

About vi
vi (pronounced vee-EYE, short for “visual”) provides basic text editing capabilities. Three
aspects of vi make it appealing----

First, vi is supplied with all UNIX systems. You can use vi at other universities or any
businesses with UNIX systems.
Second, vi uses a small amount of memory, which allows efficient operation when the network
is busy.
Third, because vi uses standard alphanumeric keys for commands, you can use it on virtually
any terminal or workstation in existence without having to worry about unusual keyboard
mappings.
As a point of interest, vi is actually a special mode of another UNIX text editor called ex.
Normally you do not need to use ex except in vi mode.

General Introduction
The vi editor (short for visual editor) is a screen editor which is available on almost all Unix systems. Once
you have learned vi, you will find that it is a fast and powerful editor. vi has no menus but instead uses
combinations of keystrokes in order to accomplish commands. If you are just beginning to learn Unix, you
might find the Pico editor easier to use (most command options are displayed at the bottom of the screen). If
you use the Pine email application and have composed or replied to a message you have probably already
used Pico as it is used for text entry. For more information please refer to the Pine/Picopage.

Starting vi
To start using vi, at the Unix prompt type vi followed by a file name. If you wish to edit an existing file, type in
its name; if you are creating a new file, type in the name you wish to give to the new file.
%vi filename
Then hit Return. You will see a screen similar to the one below which shows blank lines with tildes and the
name and status of the file.
~
~
"myfile" [New file]

vi's Modes and Moods
vi has two modes: the command mode and the insert mode. It is essential that you know which mode you are
in at any given point in time. When you are in command mode, letters of the keyboard will be interpreted as
commands. When you are in insert mode the same letters of the keyboard will type or edit text. vi always
starts out in command mode. When you wish to move between the two modes, keep these things in mind. You
can type i to enter the insert mode. If you wish to leave insert mode and return to the command mode, hit
the ESC key. If you're not sure where you are, hit ESC a couple of times and that should put you back in
command mode.

General Command Information

As mentioned previously, vi uses letters as commands. It is important to note that in general vi commands:

are case sensitive - lowercase and uppercase command letters do different things

are not displayed on the screen when you type them

generally do not require a Return after you type the command.

You will see some commands which start with a colon (:). These commands are ex commands which are used
by the ex editor. ex is the true editor which lies underneath vi -- in other words, vi is the interface for the ex
editor.

Entering Text
To begin entering text in an empty file, you must first change from the command mode to the insert mode. To
do this, type the letter i. When you start typing, anything you type will be entered into the file. Type a few
short lines and hit Return at the end of each of line. Unlike word processors, vi does not use word wrap. It
will break a line at the edge of the screen. If you make a mistake, you can use the Backspace key to remove
your errors. If the Backspace key doesn't work properly on your system, try using the Ctrl h key
combination.

Cursor Movement
You must be in command mode if you wish to move the cursor to another position in your file. If you've just
finished typing text, you're still in insert mode and will need to press ESC to return to the command mode.

Moving One Character at a Time
Try using your direction keys to move up, down, left and right in your file. Sometimes, you may find that the
direction keys don't work. If that is the case, to move the cursor one character at the time, you may use
the h, j, k, and l keys. These keys move you in the following directions:
h
left one space
l
right one space
j
down one space
k
up one space
If you move the cursor as far as you can in any direction, you may see a screen flash or hear a beep.

Moving among Words and Lines
While these four keys (or your direction keys) can move you just about anywhere you want to go in your file,
there are some shortcut keys that you can use to move a little more quickly through a document. To move
more quickly among words, you might use the following:
w
moves the cursor forward one word
b
moves the cursor backward one word (if in the middle of a
word, b will move you to the beginning of the current word).
e
moves to the end of a word.
To build on this further, you can precede these commands with a number for greater movement. For example,
5w would move you forward five words; 12b would move you backwards twelve words. [You can also use
numbers with the commands mentioned earlier. For example, 5j would move you down 5 characters.]

Command Keys and Case

You will find when using vi that lower case and upper case command keys are interpreted differently. For
example, when using the lower case w, b, and e commands, words will be defined by a space or a punctuation
mark. On the other hand, W, B, and E commands may be used to move between words also, but these
commands ignore punctuation.

Shortcuts
Two short cuts for moving quickly on a line include the $ and the 0 (zero) keys. The $ key will move you to the
end of a line, while the 0 will move you quickly to the beginning of a line.

Screen Movement
To move the cursor to a line within your current screen use the following keys:
H
moves the cursor to the top line of the screen.
M
moves the cursor to the middle line of the screen.
L
moves the cursor to the last line of the screen.
To scroll through the file and see other screens use:
ctrl-f scrolls down one screen
ctrl-b scrolls up one screen
ctrl-u scrolls up a half a screen
ctrl-d scrolls down a half a screen
Two other useful commands for moving quickly from one end to the other of a document are G to move to the
end of the file and 1G to move to the beginning of the file. If you precede G with a number, you can move to a
specific line in the document (e.g. 15G would move you to line 15).

Moving by Searching
One method for moving quickly to a particular spot in your file is to search for specific text. When you are in
command mode, type a / followed the text you wish to search for. When you press Return, the cursor will
move to the first incidence of that string of text. You can repeat the search by typing n or search in a
backwards direction by using N.

Basic Editing
To issue editing commands, you must be in command mode. As mentioned before, commands will be
interpreted differently depending upon whether they are issued in lower or upper case. Also, many of the
editing commands can be preceded by a number to indicate a repetition of the command.

Deleting (or Cutting) Characters, Words, and Lines
To delete a character, first place your cursor on that character. Then, you may use any of the following
commands:
x
deletes the character under the cursor.
X
deletes the character to the left of your cursor.
dw
deletes from the character selected to the end of the word.
dd
deletes all the current line.
D
deletes from the current character to the end of the line.
Preceding the command with a number will delete multiple characters. For example, 10x will delete the
character selected and the next 9 characters; 10X will delete the 10 characters to the left of the currently
selected character. The command 5dw will delete 5 words, while 4dd deletes four lines.

Pasting Text using Put
Often, when you delete or cut text, you may wish to reinsert it in another location of the document. The Put
command will paste in the last portion of text that was deleted since deleted text is stored in a buffer. To use
this command, place the cursor where you wish the deleted text to appear. Then use p to reinsert the text. If
you are inserting a line or paragraph use the lower case p to insert on the line below the cursor or upper
case P to place in on the line above the cursor.

Copying Text with Yank
If you wish to make a duplicate copy of existing text, you may use the yank and put commands to accomplish
this function. Yank copies the selected text into a buffer and holds it until another yank or deletion occurs.
Yank is usually used in combination with a word or line object such as the ones shown below:
yw
copies a word into a buffer (7yw copies 7 words)
yy
copies a line into a buffer (3yy will copy 3 lines)
Once the desired text is yanked, place the cursor in the spot in which you wish to insert the text and then use
the put command (p for line below or P for line above) to insert the contents of the buffer.

Replacing or Changing Characters, Words, and Lines
When you are using the following commands to replace text, you will be put temporarily into insert mode so
that you can change a character, word, line, or paragraph of text.
r
R
cw
dollar

replaces the current character with the next character you enter/type.
Once you enter the character you are returned to command mode.
puts you in overtype mode until you hit ESC which will then return
you to command mode.
changes and replaces the current word with text that you type. A
sign marks the end of the text you're changing.
finish will return you to command mode.

Pressing ESC when you

Inserting Text
If you wish to insert new text in a line, first position the cursor to the right of where you wish the inserted text
to appear. Type i to get into insert mode and then type in the desired text (note that the text is inserted before
the cursor). Press ESC to return to command mode.

Inserting a Blank Line
To insert a blank line below the line your cursor is currently located on, use the o key and then hit ESC to
return to the command mode . Use O to insert a line above the line the cursor is located on.

Appending Text
You can use the append command to add text at any place in your file. Append (a) works very much like
Insert (i) except that it insert text after the cursor rather than before it. Append is probably used most often
for adding text to the end of a line. Simply place your cursor where you wish to append text and press a. Once
you've finished appending, press ESC to go back to command mode.

Joining Lines

Since vi does not use automatic word wrap, it is not unusual in editing lines to end up with lines that are too
short and that might be improved if joined together. To do this, place your cursor on the first line to be joined
and type J. As with other commands, you can precede J with a number to join multiple lines (4J joins 4 lines).

Undoing
Be sure to remember this command. When you make a mistake you can undo it. DO NOT move the cursor
from the line where you made the change. Then try using one of the following two commands:
u
U

undoes the last change you made anywhere in the file. Using u again
will "undo the undo".
undoes all recent changes to the current line. You can not have moved
from the line to recover the original line.

Closing and Saving Files
When you edit a file in vi, you are actually editing a copy of the file rather than the original. The following
sections describe methods you might use when closing a file, quitting vi, or both.

Quitting and Saving a File
The command ZZ (notice that it is in uppercase) will allow you to quit vi and save the edits made to a file. You
will then return to a Unix prompt. Note that you can also use the following commands:
:w
:q
:wq

to save
case of
to quit
to quit

your file but not quit vi (this is good to do periodically in
machine crash!).
if you haven't made any edits.
and save edits (basically the same as ZZ).

Quitting without Saving Edits
Sometimes, when you create a mess (when you first start using vi this is easy to do!) you may wish to erase all
edits made to the file and either start over or quit. To do this, you can choose from the following two
commands:
:e!
:q!

reads the original file back in so that you can start over.
wipes out all edits and allows you to exit from vi.

More about Combining Commands, Objects, and Numbers
Now that you've learned some basic vi commands you might wish to expand your skills by trying some fancy
combination steps. Some commands are generally used in combination with a text object. We've already seen
some examples of this. For example, when you use the command dw to delete a word, that combines the delete
(d) command with the word (w) text object. When you wish to delete multiple words, you might add a
number to this combination. If you wished to delete 2 words you might use 2dw or d2w. Either of these
combinations would work. So, as you can see, the general format for a command can be
(number) (command) (text object) or (command) (number) (text object)
You might wish to try out some of the following combinations of commands and objects:
Command
Text Object
d (delete)
w (word to the left)
y (yank/copy) b (word to the right or backward)
c (change)
e (end of word)

H
L
M
0
$
(
)
[
]

(top of the screen)
(bottom of the screen)
(middle of the screen)
(zero - first character on a line)
(end of a line)
(previous sentence)
(next sentence)
(previous section)
(next section)

Repeating a Command
If you are doing repetitive editing, you may wish to use the same command over and over. vi will allow you to
use the dot (.) to repeat the last basic command you issued. If for example, you wished to deleted several lines,
you could use dd and then . (dot) in quick succession to delete a few lines.

A Quick Word about Customizing Your vi Environment
There are several options that you can set from within vi that can affect how you use vi. For example, one
option allows you to set a right margin that will then force vi to automatically wrap your lines as you type. To
do this, you would use a variation of the :set command. The :set command can be used to change various
options in vi. In the example just described, you could, while still in vi, type :set wrapmargin=10 to specify
that you wish to have a right margin of 10. Another useful option is :set number. This command causes vi to
display line numbers in the file you are working on.

Other Options
To view a listing of other options, you could type :set all. To view only those options which are currently in
effect, you can type set: by itself. Options that you set while in a vi session will apply during that session only.
To make permanent changes to your vi environment, you could edit your .exrc file. However, you should not
edit this file unless you know what you are doing!

Useful vi Commands
Cut/Paste Commands:
x
dw
dd
D
d$
:u
p,P
location
J
"[a-z]nyy
"[a-z]p/P
line

delete one character (destructive backspace)
delete the current word (Note: ndw deletes n numbered words)
delete the current line (Note: ndd deletes n numbered lines)
delete all content to the right of the cursor
same as above
undo last command
paste line starting one line below/above current cursor
combine the contents of two lines
yank next n lines into named buffer [a-z]
place the contents of selected buffer below/above the current

Extensions to the Above Commands:

:3,18d
16,25m30
23,29co62

delete lines 3 through 18
move lines 16 through 25 to after line 30
copy specified lines and place after line 62

Cursor Relocation commands:
:[n]
shift g
h/l/j/k
^f/^b
^u/^d
$
0

goto line [n]
place cursor on last line of text
move cursor left, right, down and up
move forward, backward in text, one page
move up, down one half page
move to end of line
move to beginning of line

Extensions to the Above:
b
words)
e
(
)

move backwards one word (Note: nb moves back n number of
move to end of current word
move to beginning of curent block
move to the end of current block

Searching and Substitution commands:
/ [string]
? [string]
n
N
cw
c$
c0
:1,$s/s1/s2/g
r

search forward for string
search backwards for string
repeat last search
repeat search in opposite direction
change the contents of the current word, (use ESC to stop
replacement mode)
Replace all content to the right of cursor (exit replacement
mode with ESC)
Replace all content to the left of cursor (exit with ESC)
(Yow!) global replacement of string1 with string2
replace current character with next character typed

Entering the Insert Mode:
i
I
a
A
o/O
ESC

Begin inserting text at current cursor location
Begin inserting text at the beginning of the current line
Begin appending text, one character to the right of current
cursor location
Begin appending text at the end of the current line
Begin entering text one line below\above current line
Exit insertion mode and return to command mode

Exiting and Entering VI
ZZ
:wq
:e!
:q
made)

save file and exit VI
same as above
return to last saved version of current file
quit without save, (Note :q! is required if changes have been

:w

write without exit (:w! to force write)

Fancy Stuff:
:1,10w file
:340,$w >> file
:sh
^d
:![command]
:r![command]
:r[filename]
:$r newfile
document
:r !sort file
through
:n
:^g
:set
:set
:set
:set

number
showinsert
all
ai

^T
^D
:set tabstop=n
>>
<<

write lines 1 through 10 to file newfile
write lines 340 through the end of the file and append
to file newfile
escape temporarily to a shell
return from shell to VI
execute UNIX command without leaving VI
read output of command into VI
read filename into VI
read in newfile and attach at the end of current
read in contents of file after it has been passed
the UNIX sort
open next file (works with wildcard filenames,
ex: vi file*)
list current line number
show line numbers
show flag ("I") at bottom of screen when in insert mode
display current values of VI variables
set autoindent; after this enter the insert mode and
tab, from this point on VI will indent each line to
this location. Use ESC to stop the indentations.
set the autoindent tab one tab stop to the right
set the autoindent tab one stop to the left
sets default tab space to number n
shift contents of line one tab stop to the right
shift contents of line one tab stop to the left

Notes:

1. vi is a powerful text editor; It is one of the standard Unix
editors. However, it is also very complex; entire books have
been written on vi usage and features.
2. If you do much work on Unix systems, at some time you may
have no other option; you will have to use vi.

3. This lab covers a bare minimum of vi commands; enough to
perform some very basic tasks.
4. vi was designed to work on a multitude of terminals, many of
which had no control keys and no cursor movement keys. So it
is possible to use vi using h (left) j (down) k (up) and l (right)
keys for cursor movement.
5. vi is a moded editor;
o in command mode, most keys on the keyboard represent
editing commands
o in insert mode, the keys you press insert text into your
document
Using vi:
1. To start vi, type vi <filename>
2. To get into insert mode and start entering text into the
document press:
o i - to start inserting text before the cursor
o a - to start inserting text after the cursor
o I - to start inserting text at the beginning of the current
line
o A - to start inserting text at the end of the current line
o o - to open up a new line and insert text on the new line
3. To get back to command mode:
o press [Esc]

4. To delete:
o make sure you are in command mode; press [Esc]
o x - deletes the character at the cursor
o dd - deletes the current line
o D - deletes from the current postion in the line to the end
of the line
o dG - deletes to end of file
5. To undo a command:
o u - undo the last command
6. To move a line or block of lines:
o dd - deletes the line from the current postion
o p - after moving to the new location; p will put the line(s)
after the current line
o 10dd - deletes 10 lines starting from the current postion

7. To copy a line
o yy - deletes the line from the current postion
o p - after moving to the new location; p will put the line(s)
after the current line
8. To exit:
o :q! - quit without saving

o :q - quit
o :wq - save the file and quit
o :w - save the file without quitting

History of Unix
The Unix operating system found its beginnings in MULTICS, which stands for
Multiplexed Operating and Computing System. The MULTICS project began in the
mid 1960s as a joint effort by General Electric, Massachusetts Institute for
Technology and Bell Laboratories. In 1969 Bell Laboratories pulled out of the project.
One of Bell Laboratories people involved in the project was Ken Thompson. He liked
the potential MULTICS had, but felt it was too complex and that the same thing could
be done in simpler way. In 1969 he wrote the first version of Unix, called UNICS.
UNICS stood for Uniplexed Operating and Computing System. Although the
operating system has changed, the name stuck and was eventually shortened to Unix.
Ken Thompson teamed up with Dennis Ritchie, who wrote the first C compiler. In
1973 they rewrote the Unix kernel in C. The following year a version of Unix known
as the Fifth Edition was first licensed to universities. The Seventh Edition, released in
1978, served as a dividing point for two divergent lines of Unix development. These
two branches are known as SVR4 (System V) and BSD.
Ken Thompson spent a year's sabbatical with the University of California at Berkeley.
While there he and two graduate students, Bill Joy and Chuck Haley, wrote the first
Berkely version of Unix, which was distributed to students. This resulted in the source
code being worked on and developed by many different people. The Berkeley version
of Unix is known as BSD, Berkeley Software Distribution. From BSD came the vi
editor, C shell, virtual memory, Sendmail, and support for TCP/IP.
For several years SVR4 was the more conservative, commercial, and well supported.
Today SVR4 and BSD look very much alike. Probably the biggest cosmetic difference
between them is the way the ps command functions.
The Linux operating system was developed as a Unix look alike and has a user
command interface that resembles SVR4.
 Figure 1: The Unix Family Tree

Recommended further information on the history of Unix can be found here: A Brief
History of UNIX by Mike Loukides, an editor at O'Reilly and Associates.

The Connection Between Unix and C
At the time the first Unix was written, most operating systems developers believed
that an operating system must be written in an assembly language so that it could
function effectively and gain access to the hardware. Not only was Unix innovative as
an operating system, it was ground-breaking in that it was written in a language (C)
that was not an assembly language.
The C language itself operates at a level that is just high enough to be portable to
variety of computer hardware. A great deal of publicly available Unix software is
distributed as C programs that must be complied before use.
Many Unix programs follow C's syntax. Unix system calls are regarded as C
functions.
What this means for Unix system administrators is that an understanding of C can
make Unix easier to understand.

Why Use Unix?
One of the biggest reasons for using Unix is networking capability. With other
operating systems, additional software must be purchased for networking. With Unix,
networking capability is simply part of the operating system. Unix is ideal for such
things as world wide e-mail and connecting to the Internet.
Unix was founded on what could be called a "small is good" philosophy. The idea is
that each program is designed to do one job well. Because Unix was developed
different people with different needs it has grown to an operating system that is both
flexible and easy to adapt for specific needs.
Unix was written in a machine independent language. So Unix and unix-like operating
systems can run on a variety of hardware. These systems are available from many
different sources, some of them at no cost. Because of this diversity and the ability to
utilize the same "user-interface" on many different systems, Unix is said to be an open
system.
For additional recommended information see

The software that administers the allocation of the possessions of the central processing unit
and offers people who develop that program with a suitable interface used to get along with those
possessions is known as Operating System (or simply OS).

The operating system develops the system information and the entry of user and acts in
response by assigning and organizing the jobs and interior system resources as the service to the
users and the programs of the system.

At the base of the entire system software, the operating system carries out the basic jobs
like controlling and giving priority to the system requirements, assigning the memory, making easy in
networking calculating input and output equipments, and organizing file systems.

Microsoft has designed and marketed the Windows operating system as a collection of
severaloperating systems.

Microsoft was the first to introduce the idea of an operating setting which was named as
Windows in November 1985 as an attachment to the MS-DOS in reply to the increasing curiosity in
Graphical User Interfaces (GUIs). Microsoft Windows in the end started to govern the world market
of the personal computers, going far ahead of Mac OS, which was predominating before its era.

In the year 2004 at the Directions conference of IDC, the Vice President Mr. Saxena had
declared that Windows had more or less 90% of the total customers of the operating system
market.

The latest version of Windows present in the market is Windows Vista while the
latest server version of it is the Windows Server 2003.

The descendant to Windows Server 2003 will be the Windows Server 2008 which is still in beta
version and is at present being under tested.

Research Paper help
 https://www.homeworkping.com/