SECTION 2                                                         PREV | NEXT


Using the vi Editor
    Operating Modes in the vi Editor

    Delete Commands
    Insert Commands
    Search Commands
    Copying Yanked Text
    Repeating Commands
    Joining Lines
Understanding Foreground and Background Processing
    Running a Background Job
        The nice Command
        The jobs Command
        The fg Command
        The stop Command
        The kill Command

Miscellaneous Commands
    The history Command
    The alias Command
    The cal Command
    The date Command
    The who Command
    The quota Command
    The limit Command


Using the vi Editor

Getting Started using the vi Editor

Operating Modes in the vi Editor
Aborting Commands
Saving Files

Delete Commands
Insert Commands
Search Commands
Change Commands

Marking Text
Moving Marked Text
Yank Command

Copying Yanked Text
Reading Other Files Into Current File
Repeating Commands
Joining Lines

vi is a full-view line editor. You use vi to create and modify your programs and documents when you work on a UNIX-based system. There are several ways to start the vi editor, as demonstrated below.

vi -- will invoke the editor, without opening a file

vi filename -- will invoke the editor, and open the file or create the file if it does not exist

vi /directory/filename -- will invoke the editor, and open/create the file

Operating Modes in the vi Editor

Text Input Mode

vi has two text input modes, append and insert. Typing a, from command mode, will put you in append mode (text will be added behind the cursor). Typing i, from command mode, will put you in insert mode (text will be added before the cursor).

Command Mode

All vi commands must be entered from command mode. When the vi editor is started, you are automatically placed in command mode. To get to command mode from text input mode, press <esc>. If you hear a beep when you press <esc>, you are already in command mode. Note, vi commands are case sensitive - type uppercase and lowercase characters as such.

When you press <esc> nothing appears on the screen.

Aborting Commands

To abort a command you can press <esc>. If you abort a command, you will still be in command mode.

In addition to pressing <esc>, you can type u (from command mode) to reverse the effect of the last command that changed the buffer. If you type U (from command mode), the effect of all the commands on the current line will be reversed, provided you have not moved from the line since the last change.

Also, you can correct typing errors from either of the text input modes by pressing the backspace key and typing over the incorrect text.

Saving Files

There are several ways to save files in vi, some of them are illustrated below.

:wq or ZZ or :x writes buffer to file, and exits vi
:w writes buffer to file, and stays in vi
:q exits vi without saving changes
:w filename writes buffer to filename (will not write to an existing file)
:w! filename writes buffer to filename (overwrites existing file)
:q! exits vi, and aborts all changes

 

vi has many ways of moving the cursor around in a document. Normally, the arrow keys on your keyboard will work, as long as you are in command mode. However, if you find that the arrow keys are not functioning properly you can choose any of the following commands.

h moves one character left, within current line
l moves one character right, within current line
j moves down one line
k moves up one line
b moves left to beginning of next word, or punctuation mark
B moves left to beginning of next word, ignoring punctuation
w moves right to beginning of next word, or punctuation mark
W moves right to beginning of next word, ignoring punctuation
<ctrl-f> scrolls forward one full screen
<ctrl-b> scrolls backward one full screen
<ctrl-d> scrolls forward one-half page
<ctrl-u> scrolls backward one-half page
H moves cursor to top left of screen
M moves cursor to middle left of screen
L moves cursor to bottom left of screen
G moves cursor to the beginning of the last line of file/document

In addition to the commands listed above, you can add numbers to the front of the command to make it perform its function multiple times. For example, typing 6b will move the cursor back six words.

Delete Commands

Below are just a few of the commands you can use to delete characters, words, or lines.

x deletes character at the cursor position
dd deletes current line
dw deletes one word right

You can prefix the deletion commands with numbers to process multiple functions simultaneously. For example, typing 2dd will delete the next two lines.

Insert Commands

With vi you can insert text and/or blank lines. To enter text, issue one of the following commands. Remember to press <esc>, to return to command mode.

i puts you in insert mode, insertion occurs before the current cursor position
I moves cursor to beginning of current line, and enters insert mode
o creates a blank line following the current line, moves the cursor to the beginning of the new line, and enters insert mode
O creates a blank line before the current line, moves the cursor to the beginning of the new line, and enters insert mode
a puts you in append mode, text will be added after current cursor position
A moves cursor to end of current line, and enters append mode

Search Commands

You can use the search command to help you locate a text string. If you want a match for just the characters entered as the string, be sure to include a space preceding and following the string.

/string searches forward from cursor position for string
?string searches backward from the cursor position for string
n repeats search in same direction
N repeats search in opposite direction

If you want to find all occurrences of the word ear. Enter the command "/ ear ". If you type "/ear", you will find all occurrences of the word ear, and also fear, earth, bear, etc.

Change Commands

vi has several commands that allow you to change characters, words, and lines. Some of the commands and descriptions are below.

r allows you to replace one character at current cursor position
cw allows you to replace one word at current cursor position
ctx allows you to replace text from current cursor position to a specified character x
xp transposes the letter at the cursor position with the following letter
ddp swaps position of the current line and the following line
R allows you to replace all characters beginning at the cursor position; at end of line, editor enters insert mode

You can prefix r and cw with numbers to make multiple changes.

Pasting Commands

When a string is deleted it is stored in a temporary buffer. Deleted text stays in this buffer until you modify the buffer by deleting another string or exit the editor. The text strings stored in this buffer can be removed and placed back in the text. This can be done by using the commands below.

p places last deleted text following the cursor
P places last deleted text before the cursor

Marking Text

To make a bookmark in a document place the cursor at the location where you want the bookmark; type mx (where x is any letter you choose to symbolize that particular bookmark). To return to the marked position type `x (grave accent {backward single quote} followed by the character x you specified). Typing 'x (apostrophe {forward single quote} followed by the character x you specified), will return you to the beginning of the line that contains the mark. You can have more than one bookmark in a file. Marks are forgotten when you exit vi or close that document.

Moving Marked Text

You can delete the text between your mark and the current cursor position by typing d`x (backward single quote). Then, you can move the cursor to where you want to add the text, and type p or P.

Yank Command

The yank command copies text into a temporary buffer. It does not change the original text when it makes the copy. Text remains in the buffer until you enter another command that places text into the buffer, or exit the editor.

Your original text is not deleted, just copied.

yy or Y copies the line at the cursor into the buffer
nyy or nY copies n lines from the cursor position into the buffer
yw copies the word at the cursor into the buffer
nyw copies n words following the cursor position into the buffer

Copying Yanked Text

You can use the yank command with the paste command to copy text to another location in the current document.

Reading Other Files Into Current File

You can insert another file into the current file by typing :r filename. The inserted file will be added starting on the line following the cursor position.

Repeating Commands

If you type a period (.), the last command that modified the buffer will be repeated. You can use this instead of having to retype commands.  

Joining Lines

Typing J allows you to join the current line and the following line. Typing nJ will join n lines to the current line.


Understanding Foreground and Background Processing

Running a Background Job

UNIX systems are multitasking systems. This means that you can run more than one process at the same time. A process can be anything from listing the contents of a directory to running a program.

A foreground process is any task that you run on your terminal you'll return to the command prompt only when you complete your entire task. You may have only one foreground process running at any time. You can suspend a foreground process by pressing <ctrl-z>. After suspending a foreground process, you return immediately to the command prompt.

To run multiple processes on a UNIX system, you must run them in the background. A process running in the background is called a background job. Background jobs should not require your intervention. You can run several background jobs simultaneously you are notified when a background job is completed.

Running a Background Job

The nice Command
The jobs Command
The fg Command
The stop Command
The kill Command

To run a process as a background job, follow the command for that process with an ampersand (&). The job is then assigned a job number and a unique process identification number (PID). After starting a job in the background, you'll return to the system prompt where you can execute other commands or start other jobs.

A trade off exists between running several jobs in the background simultaneously and the system response. Running several jobs in the background can significantly degrade the interactive response you receive on a UNIX system.

The nice Command

When running a process as a background job, you should precede the command for that process with the nice command. The format of the nice command used to run a process as a background job is:

nice [-n] command [options] &

where n is an optional number between 1 and 20, command is the command for that process, and options are any optional arguments for that command. The optional number included after the nice command determines the priority level of the background job the lower the number, the greater the priority. If you do not include a number with the nice command, 10 is used as the default.

If you want to be able to logout of your account and have a job continue to run in background, precede the previous command by the word nohup . The previous command would become:

nohup nice [-n] command [options] &

In the following example, the nice command is used with the FORTRAN compiler command, f77, at a nice level of 20; the object of the compiler command is the FORTRAN program called random.f (for information on compilers, please see "Running Programs on UNIX"; for information on the jobs command, see "The jobs Command"):

$ nice -20 f77 random.f & <return>
[1] 20394
$ jobs <return>
[1] + Running f77 random.f
$

The jobs Command

The jobs command displays the status of any background jobs. The format of the jobs command is simply:

jobs

In the following example, the jobs command displays the single background job running:

$ jobs <return>
[1] + Running f77 random.f
$

If you have used the nohup command to run a job in background and then logged out of your account, issuing the jobs command after you log back in will not show the status of that job. To see its status, enter the following command:

ps auxww | grep userid

If the job is still running, its status will be shown. If the job has completed, it will not appear.

The fg Command

The fg (foreground) command returns a background job to the foreground. The format of the fg command is:

fg %n

where n is the job number. If the fg command is used without any objects, the most current job in the job list is brought into the foreground.

The stop Command

The stop command stops a job currently running in the background. The format of the stop command is:

stop %n

where n is the job number.

The kill Command

The kill command cancels a job running in the background. The format of the kill command is:

kill %n or kill -9 %n

where n is the job number. Specifying the -9 option with the kill command kills the process under all circumstances.

The following examples illustrate the concept of running jobs in the foreground and background:

$ more file1 <return>
This is line number 1
This is line number 2
.
.
.
--More--(57%)
<ctrl-z>
Suspended
% ls -l <return>
total 8

-rw------- 1 jsmith 1865 Nov 19 12:00 file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05 file2
-rw----rw- 1 jsmith 865 Dec 25 05:00 file3
drw-rw---- 2 jsmith 512 May12 11:23 letters

<ctrl-z>
Suspended
$ cat file1 file2 > file3 & <return>
[3] 5421
$ jobs <return>
[1] Suspended more file1
[2] - Suspended ls -l
[3] + Running cat file1 file2 > file3
$ diff file1 file2 > file.dif & <return>
[4] 5427
$

The first number represents the job number and the second number is the unique process identification number (PID) assigned to the job.

$ jobs <return>
[1] Suspended more file1
[2] Suspended ls -l
[3] - Running cat file1 file2 > file3
[4] + Running diff file1 file2 > file.dif
$
[3] Done cat file1 file2 > file3
$

As shown in the above example, you are informed when any background job finishes.

$ jobs <return>
[1] Suspended more file1
[2] - Suspended ls -l
[4] + Running diff file1 file2 > file.dif
$ fg %1 <return>
more file1
This is line number 1
This is line number 2
.
.
.q
$ kill %2 <return>
[2] Terminated ls -l
$ kill %4 <return>
[4] Terminated diff file1 file2 > file.dif
$ jobs <return>
$

If you try logging out and you have any stopped jobs, you are notified by the system of those jobs. If you do not need the stopped jobs, you should kill them. In either case, you can use the logout command or <ctrl-d> a second time to log out.


Miscellaneous Commands

The history Command
The alias Command
The cal Command
The date Command
The who Command
The quota Command
The limit Command

The history Command

The history command displays the last 20 commands used (by default), but this number can be increased or decreased.

You can reissue a previous command with one of the following commands:

!! -- Execute the last command entered.
!n -- Execute command number n on the history list.
!xxx -- Execute the most recent command on the history list starting with xxx.
!-n -- Execute the nth command ago.

The following example shows the history command being used:

$ history <return>
1 more file1
2 pwd
3 cd /
4 cd
5 history
$ !2 <return>
pwd
/home/dept/jsmith
$ history <return>
1 more file1
2 pwd
3 cd /
4 cd
5 history
6 pwd
7 history
$ !-7 <return>
more file1
This is line number 1
This is line number 2
.
.
.q
$ history <return>
1 more file1
2 pwd
3 cd /
4 cd
5 history
6 pwd
7 history
8 more file1
9 history
$ !c <return>
cd
$ pwd <return>
/home/dept/jsmith
$

The alias Command

The alias command is used for making a nickname or abbreviation for a UNIX command. You can consequently designate your own name for any UNIX command. The format of the alias command is:

alias nickname UNIX_command

To display a list of all current aliases, use the alias command with no object. In the following example, the alias command is used first to nickname the more command as type and then the ls command, including the -l (long) option, as long:

$ alias type more <return>
$ alias long 'ls -l' <return>
$ type file1 <return>
This is line number 1
This is line number 2
.
.
.q
$ long <return>
total 8

-rw------- 1 jsmith 1865 Nov 19 12:00 file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05 file2
-rw----rw- 1 jsmith 865 Dec 25 05:00 file3
drw-rw---- 2 jsmith 512 May12 11:23 letters

$ alias <return>
type more
long ls -l
%

The cal Command

The cal (calendar) command displays the calendar for the specified month and year. The format of the cal command is:

cal month year

If you exclude the month number from the cal command, the calendar for the entire specified year is displayed.

$ cal 4 1992 <return>

April 1992

S M Tu W Th F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

$

You must specify the entire year with the cal command. For example, cal 90 displays the calendar for 90 AD, not 1990. To display the calendar for 1990, you must use cal 1990. The cal command with no object displays the calendar for the current month.

The date Command

The date command displays the current date and time on your screen:

$ date <return>
Thu Aug 28 08:56:28 CDT 1997
$

The who Command

The who command displays all users currently logged in to a UNIX system. The who command displays the user's login name, the device to which the user's terminal is connected, and the login time and date.

In the following example, the who command shows three users currently logged in:

$ who <return>

opr5 console Aug 20 07:54
jsmith ttyp0 Aug 20 09:38 (129.107.1.100)
bcarson ttyp3 Aug 20 10:43 (129.107.1.100)

The quota Command

The quota command displays the disk quota for your account. To check your disk quota, type the following:

$ quota <return>
Disk quotas for user sbh2284 (uid 4210):

Filesystem blocks quota limit grace files quota limit grace
/home 1084 4096 6144 84 0 0

The limit Command

The limit command shows the maximum computing resources that can be allocated to a process. To see this listing, type the following:

$ limit <return>
cputime 3:00:00
filesize unlimited
datasize 81920 kbytes
stacksize 2048 kbytes
coredumpsize unlimited
memoryuse 1022184 kbytes
vmemoryuse 1048576 kbytes
descriptors 96