Previous Table of Contents Next

Using vi

The visual editor, vi, is commonly used by system administrators to edit text files. Whole books have been written about using vi. This section provides only a quick-reference table of some of the most commonly used editing commands.

To start vi, type vi filename and press Return. If the file does not exist, a new file is opened. The new file is created when you save changes made to it. If the file exists, the beginning of the file is displayed.

Table 2-1 shows a few of the many vi editing commands.

Table 2-1 Some Basic vi Commands

Task Command
How to save /quit a file
Quit without saving changes :q!
Write changes :w
Write changes and quit :wq
Write changes and quit ZZ
How to move around in a file
Move cursor one character left h
Move cursor one character right l
Move cursor up one line k
Move cursor down one line j
Go to end of the file G
How to add text
Insert text (insert mode) i text Esc
Append text at cursor location a text Esc
Append text at end of the line A text Esc
How to exit to command mode Esc
How to make changes to a file
Delete line dd
Delete character x
Delete word dw
Open new line above O text Esc
Open new line below o text Esc
Yank/copy line Y
Put before P
Put after p

Combining Commands and Redirecting Output

The SunOS 5.x system software lets you combine commands in several ways. This section describes the three ways you can combine commands.

Typing Several Commands on the Same Command Line

You can type more than one command on a single command line by typing a semicolon (;) between the commands.

For example, you can change to a directory and list the commands by typing cd/usr/bin;ls and pressing Return. Another example is setting an environment variable for the Bourne shell and then exporting the variable:

PATH=.:/usr/bin:$HOME/bin;export PATH

Redirecting Output (<>)

Unless you indicate otherwise, commands normally display their results on the screen. You can, however, redirect the output of a command using the redirect symbols < and >. For example, to save the output to a file instead of displaying it on the screen, use the > redirect symbol to tell the shell to put the contents in a file. In this example, the output of the date command is redirected to a new file called sample.file:

$ date > sample.file

Here are the contents of sample.file:

$ more sample.file
Tue May 26 13:26:59 PDT 1992

You can also redirect input in the other direction. For example, to mail the contents of a file to user ignatz@oak, type mail ignatz@oak < report.file and press Return. The file called report.file is sent by electronic mail to ignatz@oak.

Combining Commands (|)

You can use the pipe (|) operator to connect two or more commands, using the output from one command as the input to the next one. This section provides only two examples of the many ways you can combine commands in a pipeline.

To print the cat(1) manual page, type man cat | lp and press Return. The manual page is not displayed on the screen. Instead, the output is sent to the lp command, which prints it on the default printer.

You can search the process list for a particular command by piping the output of ps -e to the grep command. The output is displayed on the screen. For example, to display process information for OpenWindows:

cinderella% ps -e | grep openwin
   26Ø ?        Ø:ØØ openwin

If you want to print the information, you can add an additional pipe command (| lp) to the end of the sequence and send it to the printer:

anastasia% ps -e | grep openwin | lp
request id is castle-51 (request id is castle-51 (standard input)

Using Manual Pages

Manual pages are on-line technical references for each SunOS 5.x command. Manual pages are grouped into sections, with similar types of commands within the same section. For example, most user commands are in section (1), and system administration commands are in section (1M). Manual pages may be installed on a local system, or NFS mounted from a server. This section tells you how to display manual pages and how to find out the section numbers for an individual command.

Displaying a Manual Page (man)

To display a manual page, type man command-name and press Return. The manual page is displayed:

cinderella% man grep

grep(1)    USER COMMANDS   grep(1)

  grep - search a file for a pattern

     grep   [   -bchilnsvw   ]  limited-regular-expression    [

(More information not shown in this example)

Previous Table of Contents Next