Vi Editor Short Commands
Vi Editor Short Commands
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 Linix systems, at some time you may have no other option; you will
have to use vi.
3. 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.
4. 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
o p - after moving to the new location; p will put all 10 lines after the current line
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
Exercises:
1. 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
2. Edit the copy of the password file as follows:
a. Find the line for nobody. In this line:
i. Change the username to joe.
ii. Change the userid to 500.
b. Find the line for the games 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 news 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 ii account.
f. Save your work.
3. Using vi editor create a new file viIntro. Write following text in the file :
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/
4. For following questions, assume that you are editing the file viIntro, you are in command
mode and the cursor is on line 2, word 2 under the i.
a) What happens if you type: dd
Answer: the second line will be deleted.
b) 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
c) 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
d) What command do you use to save the file and quit?
Answer: :wq
e) 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>