Consultor Eletrônico



Kbase P109483: General instructions for UNIX vi editor.
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/16/2008
Status: Unverified

GOAL:

General instructions for UNIX vi editor.

GOAL:

What is the UNIX visual editor?

GOAL:

What screen editor is available on UNIX system?

GOAL:

Beginners guide to visual editor on UNIX.

FIX:

The vi editor is a screen editor which is available on almost all UNIX systems. This editor has no menus but instead uses combinations of keystrokes in order to accomplish commands.

A. Starting vi:

To start using vi, at the UNIX prompt type vi followed by a file name. To edit an existing file, type in it's name; if a new file is being created, type in the name of the new file. i.e.,
% vi myfile

Then hit the return key. A screen similar to the one below which shows blank lines with tildes and the name and status of the file will be presented.

~

~

"myfile" [New file]

vi's Two Modes:

vi has two modes; the command mode and the insert mode. It is essential to know which mode is in use in any given point in time. When in command mode, letters on the keyboard will be interpreted as commands. When in insert mode the same letters of the keyboard will type or edit test. vi always starts out in the command mode.

to move between the two modes:

1. Type i to enter the insert mode.

2. To leave the insert mode, hit the ESC key. If unsure which mode is in use, hit ESC key a couple of times.


B. General Command Information:

Commands are:
1. Case sensitive, lower case and upper case command letters perform differently.

2. Not displayed on the screen when entered.

3. Do not require a return entry.
Entering Text:

To begin entering text in a empty file, first change from the command mode to the insert mode. To do this type the letter i. When typing begins, anything keyed will be entered into the file. Unlike word processors, vi does not use work wrap. It will break a line at the edge of the screen. If a mistake is made, use the backspace key to remove errors.


C. Cursor Movement:

Only command mode allows the cursor movement to another position in the file. Upon completion of text input, press ESC to return to the command mode.

Moving One Character at a Time:

Should the direction keys fail to move up, down, left or right, vi permits the cursor to move one character at a time via the h, j, k, and l keys. These keys move in the following direction:

h left one space
j down one space
l right one space
k up one space.


Moving Among Words and Lines:

While the above four direction keys will move just about anywhere within the file, there are some shortcut keys that move a little more quickly through a document. They as follows:

w moves the cursor forward one word
b moves the cursor backward one word, if in the middle of a word, b will move you to the beginning of the current word
e moves to the end of a word

To build on this further, precede the above commands with a number for greater movement. For example, 5w would move forward 5 words.

Screen Movement:

To move the cursor to a line within the current screen use the following keys:

H moves the cursor to the top line of the screen
M moves the cursor to the middle of the screen
L moves the cursor to the last line of the screen.


D. Basic Editing:

To issue editing commands, vi must be in command mode. As mentioned previously, commands will be interpreted differently depending upon whether they are issued in lower or upper case. Also, many of the editing commands can be preceded by a number to indicate a repetition of the command.

Deleting Characters, Words and Lines:

To delete a character, first place the cursor on that character. Then, use any of the fo.llowing commands:

x deletes the character under the cursor
X deletes the character to the left of cursor
dw deletes from the character selected to the end of the word
dd deletes all the current line
D deletes from the current character to the end of the line

Replacing or Changing Characters, Words, and Lines:

When using the following commands to replace text, vi will be placed temporarily into insert mode so that a change to a character, word, line or paragraph of text will be permitted.

r replaces the current character with the next character you enter/type. Once the character is entered vi will returned to command mode
R placed into overtype mode until ESC key is pressed.
cw changes and replaces the current word with text that is entered. A dollar sigh marks the end of the text being changed. Pressing ESC when
finished will return to command mode


E. Closing and Saving a File:

When a file is edited in vi, vi is actually editing a copy of the file rather than the original. The command ZZ will allow exit of vi and save the edits made to a file. A UNIX prompt is then presented.

The following commands may also be used:

:w! to save the file but will not quit vi
:q! to quit if no edits have been made or ignore changes that have been made
:wq! to quit and save edits; basically the same as ZZ
.