A very brief introduction to vi
Notes:
-
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.
-
If you do much work on Unix systems, at some time you may have no other option;
you will have to use vi.
-
This lab covers a bare minimum of vi commands; enough to perform some very
basic tasks.
-
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.
-
vi is a moded editor;
-
in command mode, most keys on the keyboard represent editing commands
-
in insert mode, the keys you press insert text into your document
Using vi:
-
To start vi, type vi <filename>
-
To get into insert mode and start entering text into the document press:
-
i - to start inserting text before the cursor
-
a - to start inserting text after the cursor
-
I - to start inserting text at the beginning of the current line
-
A - to start inserting text at the end of the current line
-
o - to open up a new line and insert text on the new line
-
To get back to command mode:
-
To delete:
-
make sure you are in command mode; press [Esc]
-
x - deletes the character at the cursor
-
dd - deletes the current line
-
D - deletes from the current postion in the line to the end of the line
-
dG - deletes to end of file
-
To undo a command:
-
u - undo the last command
-
To move a line or block of lines:
-
dd - deletes the line from the current postion
-
p - after moving to the new location; p will put the line(s) after the current
line
-
10dd - deletes 10 lines starting from the current postion
-
p - after moving to the new location; p will put all 10 lines after the current
line
-
To copy a line
-
yy - deletes the line from the current postion
-
p - after moving to the new location; p will put the line(s) after the current
line
-
To exit:
-
:q! - quit without saving
-
:q - quit
-
:wq - save the file and quit
-
:w - save the file without quitting
Exercises:
-
Use vi to create a file with the answers to these questions:
-
what is command history?
-
what is command completion?
-
which symbol is used to represent the root directory?
-
what type of files are stored in the /etc directory?
-
Copy the file /etc/passwd to your own home directory.
This file is the user database file. It contains one line for each user;
each line contains seven fields separated by colons. The fields are:
username:password:userid:groupid:name:home_directory:shell
Edit the copy of the password file as follows:
a. Find the line for your account. In this line:
i. Change the username to joe.
ii. Change the userid to 500.
b. Find the line for the alterego account.
i. Change the name to Dr. Freud.
ii. Change the groupid to 600.
iii. Change the shell to /usr/bin/flin.
c. Find the line for the floopy account.
i. Change the password to *.
d. Add a new line: me:*:1004:100:me:/home/me:/bin/bash
e. Delete the line for the games account.
f. Save your work.
Questions & Answers:
For each question, assume that you are editing this file, you are in command
mode and the cursor is on line 2, word 2 under the i.
There is a site on the Internet titled:
Unix is a four letter word and vi is a two letter
abbreviation.
The URL for this site is: http://www.linuxbox.com/~taylor/4ltrwrd/
-
What happens if you type: dd
Answer: the second line will be deleted.
-
What happens if you type xiwa
Answer: x deletes the letter over the cursor; i switches to insert
mode; the letters wa are inserted.
The start of the line becomes: Unix was a four letter word
-
What happens if you type oC.C.Taylor
Answer: A new line opens up under line 2. The new line will contain
the text: C.C.Taylor
-
What command do you use to save the file and quit?
Answer: :wq
-
What does it mean if you get the error message "File is temporary, exit will
discard modifications" when you try to save and quit?
Answer: You did not give a filename when you started vi. You will
have to save the file with the command: [ESC]:w <filename>
Copyright © L.
M. MacEwan