Visual Editor (vi)

Visual Editor (vi)

vi is a text processing tool that carries out the following tasks on Solaris and other UNIX systems:

  • Creates new text files

  • Modifies existing files

  • Searches for a text string in a file

  • Replaces one string with another in a file

  • Moves or copies a string within a file

  • Removes a string from a file

To run vi from the command line and create a new file, the following command can be used:

$ vi

To run vi from the command line and edit an existing file, the following command can be used:

$ vi file.txt

where file.txt is the filename of the file to be edited. The result of editing a file (such as /etc/passwd.txt) is shown in Figure 16-1.

Click To expand Figure 16-1: Editing the /etc/passwd file.

When editing an existing file, the visual editor copies the bytes from disk into a member buffer, which is then operated on according to user commands. Text can be inserted during edit mode, while commands are executed during command mode. Changes are not written to disk until the appropriate save command is executed. During edit mode, special keys like the arrow keys will not operate as commands to move the cursor around the screen; instead, the actual code will be inserted into the file. These keys can only be used when the editor is in command mode. During edit mode, you can switch to command mode by pressing the ESCAPE key. When in command mode, you can switch to edit mode by pressing the I key.

The following commands can be executed in command mode:

  • / Performs a forward search for a text string.

  • ? Performs a backward search for a text string.

  • : Runs an ex editor command on the current line.

  • ! Executes a shell within vi.

  • ZZ Saves a file and exits.

  • h Moves the cursor left.

  • j Moves the cursor down.

  • k Moves the cursor up.

  • l Moves the cursor right.

  • nG Moves cursor to line n.

  • w Moves to next word.

  • b Moves back one word.

  • dw Deletes words.

  • ndw Deletes n words.

  • d^ Deletes all words to the beginning of the line.

  • dd Deletes the current line.

  • dG Deletes all lines to the end of the file.

  • D Deletes all words to the end of the line.

  • x Deletes the current character.

  • nx Deletes n characters to the right.

  • nY Yanks n lines into the buffer.

  • p Pastes to the right of the cursor.

  • P Pastes to the left of the cursor.

    EXAM TIP  

    You should be able to identify the functions of each of these commands for the exam.

A separate set of commands, called the ex commands, can be run by using the colon in conjunction with one of these commands:

  • :n Moves cursor to line n.

  • :$ Moves cursor to the end of the file.

  • :%s/a/b/g Replaces all occurrences of string a with string b.

  • :wq Saves modified file and quits.

  • :q! Quits without saving any changes.

  • :set Sets a number of different options.

Let’s examine the result of using the ex command :%s/a/b/g. Figure 16-2 shows the /etc/passwd with an ex command that will search for all occurrences of "export" and replace them with "staff".

Click To expand
Figure 16-2: Using an ex command.

Figure 16-3 shows the result output. The string /export/home now changes to /staff/home.

Click To expand
Figure 16-3: Performing text substitutions.


Part I: Solaris 9 Operating Environment, Exam I