UNIX environments
 
 

* The vi text editor

When you use UNIX, you will probably encounter a need for a text editor at some point. UNIX comes with several simple text editors built into the system. If you don't like the editors that are available, you can download other editors that better suit your needs.

We'll be covering the vi editor. The name of this editor is pronounced vee-eye, just like the letters that make up its name. vi is fairly standard across different flavors of UNIX and is universally available.

Opening a new file with vi

The basic vi editor looks like this when a new file is opened. At this point, vi is waiting for input from you:

Input commands

There are two modes of operation for vi. One is called command mode and the other is called insert mode. When vi opens, it is automatically placed in command mode.

Command mode does not allow you to enter text. It does allow you to move around the file. Since our file is empty, we can't navigate around the screen.

To add text to the file, we must switch to insert mode. There are several ways to do this.

a Appends text after the cursor
A Appends text at the end of line
i Inserts text before cursor
I Inserts text at the beginning of the line
o Opens a new line below the cursor
O Opens a new line above the cursor

For a brand new file, it doesn't really matter which of these you choose.

To enter insert mode, press [i]. Once you have entered insert mode, the editor may tell you at the bottom of the screen that you are now in insert mode. Once you start typing, however, this indicator will go away. It can sometimes be difficult to keep track of what editor mode you're in.

Pressing [Esc] will automatically switch you back to command mode. You cannot enter text now, but you can position the cursor anywhere you like by using a number of keys. To re-enter insert mode, position your cursor at the end of the line you've typed and press [a]. All text you enter will be placed to the right of your cursor.

Positioning commands

If you are in command mode, you can use the keyboard to position your cursor. There are several ways to position your cursor with keyboard commands. Let's examine some of them.

h, f or bkspc Moves the cursor one character to the left
j or i Moves the cursor down one line
k or h Moves the cursor up one line
l , g or spacebar Moves the cursor one character to the right
w Moves the cursor one word to the right
W moves the cursor one word past punctuation to the right.
b Moves the cursor back one word
B Moves the cursor back one word past punctuation
e Moves cursor to the end of the current word
Return Moves the cursor down one line
H Moves the cursor to the top of the screen
M Moves the cursor to the middle of the screen
L Moves the cursor to the bottom of the screen
ctrl-f Pages forward one screen
ctrl-d Scroll down one half screen
ctrl-b Page back one screen
ctrl-u   Scroll up one half screen

Delete commands

vi has a number of delete commands you can use. Let's look at these:

x Deletes the character underneath the cursor
X Deletes the character to the left of the cursor
dw Deletes the word to the right of the cursor
#dw Deletes the number of words indicated. 3dw would delete threewords
dd Deletes the line containing the cursor
#dd Deletes the number of lines indicated
D Deletes to the end of the line
dG Deletes to the end of the file
d1G Deletes from the beginning of the file to the cursor
:5, 10d Deletes lines 5 through 10

Editing commands

vi has several other editing commands that may be useful. Let's look at these:

J Join the current line with the line below
xp Transpose the character at the cursor and the character to theright.
~ Change the case of the letter underneath the cursor
u Undo the previous command
U Undo all changes to the current line
:u Undo previous last-line command

Copying and pasting commands

vi has several commands to allow you to copy and paste text into your vi file.

yy "Yank" or copy line
p Put "yanked" line below current line
P Put "yanked" line above current line
:1,3 co 5 Copy lines 1-3 and insert after line 5

Saving a file

Once you have created a file, you will want to save it and return to the shell. There are several command options at this point. Remember: you must be in command mode to save a file. Enter command mode by pressing [Esc].

Also, if you have not yet given your file a name, the file name must follow any of the write commands.  For example, to write your file to the disk and quit, after entering command mode you would type:

:wq myfile

Remember: you must start your command with a colon. vi will tell you that it's written a file to disk and will return you to the shell program.

:w will write the file to disk
:wq Will write the file to the disk and quit vi
:q! Will quit vi without writing the file.

The best way to learn vi is to practice with it. There are many commands and functions in this editor that are not intuitive. You will need to work with them to make the most of vi. Consult the exercises section for vi for more practice with the editor.