vi Editor
The vi program is a full-screen text editor that can almost certainly be found on all UNIX systems. vi does not use a large amount of system resources and yet it is very powerful. We are only going touch on vi basics. This way we will be able to create and edit files in the coming lessons.
There are two modes in vi. The first is input mode. In input mode, text is entered in the document. You can insert or append text. The second mode is command mode. When vi is in command mode, you can move within the document, merge lines, search, cut, and so on. You can carry out ALL of the functions of vi from command mode except enter new text. Text can only be entered when in text mode. So, let's get started!
- Log on to your UNIX account. Type in "vi myfirstscript".
- Hit the "i" on your keyboard. You are now in input mode.
- Please type the following text into the editor. This text is what is known as a shell script- a small program that can be executed. Please make sure to break the lines up exactly as they are here. You can type in the
text and delete the text using your backspace key.
#!/bin/sh
echo "You're running `uname -s` version `uname -r`"
for x in ash bash bsh csh ksh pdksh sh tcsh zsh; do
test -x /bin/$x && shells="$shells $x"
done
echo "You have at least the following shells installed:$shells"
- Hit the ESCAPE key to put yourself in command mode.
- Move your cursor to the word "at" after the second echo
statement. In order to do this you should use some of these commands.
"l" moves your cursor right, "h" moves your cursor
left, "k" moves your cursor up, and "j" moves.
Practice moving around until you get to the first "at" in
this script.
- Is your cursor placed before the word? Place it after the word by
hitting the the "w" key. Now, hit the "b" key.
Pretty neat, isn't it? Play around for a minute and then bring yourself
back to the beginning of the word "at."
- Now, press "dd". Oops! We erased the whole line! Type
"u". It is fixed!
- Make sure you are located right at the word "at" and press
"dw". This should delete the word. Press the period key
on your keyboard to repeat the previous command.
- Now, let's save the document. Press the ESC key to bring yourself
back to command mode. Then press "ZZ" to save and quit.
Common vi Commands
| ESC |
Puts you in command mode |
| h, j, k, l |
Left, down, up, right (or use the arrow
keys) |
| w, W, b, B |
Forward, backward by word |
| 0, $ |
First, last position of current line |
| /pattern |
Search forward for pattern |
| ?pattern |
Search backward for pattern |
| n,N |
Repeat last search in same, opposite direction |
| x |
Delete character |
| dd |
Delete current line |
| D |
Delete to end of line |
| dw |
Delete word |
| p, P |
Put deleted text before, after cursor |
| u |
Undo last command |
| . |
Repeat the last command
|
| i, a |
Insert text before, after cursor [Puts you into INPUT
MODE] |
| o, O |
Open new line for text below, above cursor [Puts
you into INPUT MODE] |
| ZZ |
Save file and quit |
| :w |
Save file |
| :q! |
Quit, without saving changes |
There are many, many more commands and functions available in vi. The following website has an excellent vi tutorial. UNIX admistrators who are adept at vi have starting salaries of over $156,000! So, get learning.
http://ecn.www.ecn.purdue.edu/ECN/Documents/VI/
Now for your Lab Assignment:
Go to the website above and find 6 commands & explanations
that you think would be useful. Do not include any that are in the table above. Post them to the bulletin board for comment.
NEXT LESSON
|