Working with Vi editor in AIX


The visual (Vi) editor is a screen editor that comes with the AIX operating system. The vi program is used to create and edit text files. Initially, the editor is in command mode. Vi is based on the ex line editor, but unlike ex, vi will allow you to use the arror keys if you are using (or emulating) a terminal that provides support for them. If your terminal does not support the full screen mode (eg. using TELNET from the IBM 4381), you may still edit text by issuing the appropriate commands.

When you edit a file, vi makes a copy of the file to a memory buffer. This way, if any unwanted changes are made, the original file is not destroyed. When your work is completeed, you must remeber to save the buffer which contains your changes. The visual editor has two modes of operation: text input mode and command mode.

The vi editor can be executed by issuing the command:

         vi [-l] [-R] [+n | +/pattern] [filename]
 
where filename is the name of the file to be edited or created, n is the line number on which to start the display, and pattern is a string specifying which line on which to start. You will then be placed in command mode. The -l option tells vi to create indents appropriate for LISP code. Passing the -R option tells vi to open the file read-only -- the same as if the view command was used. When editing a file, vi places a tilde (~) as the first character of lines past the end of your file. These lines are not part of your file, and are for display purposes only.

Using vi Editor

Moving the Cursor

There are over forty commands that can be used to move the cursor throughout the file. The table below will show some of the more common key sequences and their actions on the cursor. To repeat a command, type the number of times the command is to be performed and then press the command key sequence.

    Key               Action               Key                Action
-------------------------------------------------------------------------------
h or [Left Arrow]  Left one character   l or [Right Arrow]   Right one character
j or [Down Arrow]  Down one line        k or [Up Arrow]      Up one character

^    1st character on current line           $  End of current line
0    1st position of current line            G  Last line of the file
+    1st character of next line             :n  Line n of the file
-    1st character of previous line          H  Top line of screen
nH   n lines after top line                  M  Middle line of screen
nL   n lines before last line                L  Last line of screen

B  Back one word (ignore punctuation)        b Back one word
E  End of next word (ignore punctuation)     e End of next word
W  Start of next word (ignore punctuation)   w Start of next word
(  Beginning of previous sentence            ) Beginning of next sentence
[[ Back one section                         ]] Forward one section
{  Beginning of previous paragraph           } Beginning of next paragraph

[CTRL][B]   Scroll back one screen      [CTRL][F] Scroll forward one screen
[CTRL][U]   Scroll back half screen     [CTRL][D] Scroll forward half screen
[CTRL][E]   Scroll up one line          [CTRL][Y] Scroll down one line

Marking Text and Returning

Facilities are provided for placing a mark within a line of text. You can then move the cursor to that mark with a few simple keystrokes. To place a mark at the current cursor position, use the m command followed by a single, lowercase letter. This letter will be the mark placed in the line. Later when you wish to return to the mark, enter the ` command followed by the mark character. To go to the beginning of the line which contains the mark, use the ' command followed by the mark character. If you moved a block of text, you can return to the previous mark by issuing the " command.

Text Input Mode

Text input mode can be used to enter and delete new text in your file. Below is a table showing the various key sequences to place you into text input mode as well as those to return to command mode and perform operation while in text input mode. When you are in text input mode, everything you type will be placed into your document until you press the [ESC] key.
Key        Action                       Key       Action
-------------------------------------------------------------------------------
a          Insert after cursor        A           Append to end of line
i          Insert before cursor       I           Insert at beginning of line
o          Insert line below cursor   O           Insert one line above cursor
[CTRL][J]  Move down one line         [CTRL][W]   Move back one word
[ENTER]    Add a new line             [Backspace] Move back one character
kill       Delete line (set by stty)  [CTRL][H]   Move back one character
[CTRL][I]  Insert a tab               [CTRL][T]   Move to next tab setting
[ESC]      Returns to command mode    [CTRL][V]   Quote next character

Deleting and Changing text

These commands let you change or delete the text that is currently in your file. Each command can be repeated by entering the number of times the command is to be performed before entering the command. The table below shows some of the more common editing commands:
Key  Action                          Key  Action
-------------------------------------------------------------------------
cw   Change word                     cc   Change line
C    Change text from current        cl   change character right of cursor
     position to end of line         .    Repeat last change
dd   Delete current line             ndd  Delete n lines
D    Delete remainder of line        dw   Delete a word
d}   Delete to next paragraph        d^   Delete back to beginning of line
dh   Delete char left                d/pattern  Delete up to pattern
dn   Delete up to pattern            dL   Delete up to last line on screen
dfa  Delete up to and                dta  Delete up to a
     including a
dG   Delete to end of file           ~  Reverse case
p    Inserts last deleted text       P  Inserts last deleted text before cursor
     after cursor
rx   Replaces character with x       Rtext   Replaces text beginning at cursor
s    Substitute character            S  Substitute entire line
x    Delete current cursor position  X  Delete back one character

Moving and Copying Text

You can also move and copy text within a file by use of a temporary buffer in vi. When you issue one of the deletion commands mentioned in the previous section, vi places the deleted text in a temporary buffer. You can then place that text after or before cursor position by issuing the p or P command respectively. Sometimes, however, you want to copy the text to a new location. To save you from having to delete the text and put it back from where it was deleted, vi provided a series of text moving functions.
Pressing Y or yy will copy the current line to the buffer. The y]] command will copy the text up to the next section heading. You can also copy to the end of a word by using the ye command.

The vi editor also provides a series of numbered buffers that you can use for storing multiple selections of text. To use these buffers, type a " followed by the buffer number before the yy, d, or p commands. This will respectively yank the current line, delete the character, and put the contents of the specified buffer into the file. Since buffer one is used by vi for normal deletion, you may wish to use a safer buffer when moving or copying text. This can be done by using a lower case letter as the buffer number. As an example, to yank the next 3 lines of text and place them in buffer r you would type:

"r3yy

Searching for Text

The vi editor also provides an extensive set of search commands while in command mode. You can search forwards and backwards for a specific character by using the f and F commands respectively. The character for which you are searching should be specified after the search command. You can repeat the last character search by pressing the ; key. Pressing the , will reverse the search direction for character searches.
If you are programming in LISP or writing equations, you may find it useful to match your parenthesis and brackets. This can be done in vi by use of the % command. This command will find the match for the current brace, bracket, or parenthesis.

You can also search for a text string by placing a / in front of the text for which you are searching. The vi editor will then place the cursor at the first location after the cursor of the specified text. To search backwards from the cursor, use the ? instead of a /. You can repeat the previous search by pressing n, repeat the search in the opposite direction with the N command, repeat the forward search with the / command, and using ? will repeat the previous backwards search command.

Aligning Text You can tell vi to shift your text 8 spaces to the right or left by use of the > > and < < commands respectively. If you wish to shift your text from the current line to the end of the screen, use the > L and < L commands. You can also join two lines by using the J command. This command will place the next line at the end of the current line, wrapping the text to the next line as necessary. If you want to break up a line into two or more lines, place the cursor at the position where the line is to be broken, and insert a carriage return ([ENTER]).

Undoing Changes

Sometimes you will make a change and then immediately wish you could undo it. Such a wish is possible with vi. To undo an action, you must be in command mode. Pressing u will undo the last command entered. Pressing U, will cause vi to undo all the changes made to the current line.

Saving and Leaving vi

There are several ways to quit vi both with and without saving your file. Each of these commands should be entered in the command mode. If you wish to save your file and continue working, use the :w command. If you have not given your file a name you should do so by specifying the filename after the :f command. Then you can use :w to save the file. If you want to save the file under a different name, you can specify it after the :w command. Specifying an ! after the :W command will force vi to overwrite an existing file if the changed name represents a file that already exists. You can also write specific lines to a file by specifying the start and ending line numbers (separated by a comma) between the : and the w. For example, the command 
      :3,25w partial.file 
 
will save lines 3 through 25 to the partial.file file.
If you want to save your work and exit the editor, you can do that by issuing the ZZ or the :wq command. If you wish to quit vi without saving and you have not made any changes to the file, you can use the :q command. If you have made changes to the file, but you wish to exit vi without saving them, use the :q! command.

Using ex Commands

The vi editor is the open/visual mode of the ex editor. Therefore, when using vi, you can easily switch to ex's command mode. The ex editor provides some commands which are unavailable in vi. For example, the commands to perform global search and replace operations. To get a list of ex commands, please see the appropriate man pages. To switch to ex while you are editing a file, press Q. This will place you in ex's command mode. When you wish to quit ex and return to vi, type the vi command.

Editing Multiple Files

The vi editor provides facilities for editing several files. To edit a new file while in vi, enter the :e command followed by the name of the file. The current file becomes the alternate file if no changes have been made, otherwise, the new file becomes the alternate file. To switch to the alternate file, enter the command :e # or press [CTRL] [A] . If you wish to discard the changes in the current file and reload the last saved version, issue the :e! command. You can also specify the line on which to start editing by specifying a + followed by a line number between the :e command and the filename.
You can make a list of files to edit by placing each filename after the :n command. To edit the next file in the list, issue the :n command.

Including Text from Files and Command Output

It is also possible to insert a file into the current file. The editor will place the new file after the cursor. To insert the file, use the :r command followed by the name of the file to be inserted. The editor also lets you insert the output of an AIX command into your document. To do this, issue the :r ! command followed by the name of the AIX command to run. You can also specify which line of the output to start including by placing it between the : and the r.

vi Editor Tips

  • You can suspend the process running vi by pressing [CTRL] [Z] . This will bring you back to your OS prompt. To return to vi, make the process the foreground process with the fg command.
  • You can issue AIX commands from within vi by typing :! followed by the command.
  • You can create a subshell by issuing the :sh command. When you are done, press [CTRL] [D] or type EXIT [ENTER] to return to vi.
  • You can move the cursor to a specific column by specifying the column number and then the vertical bar ( | ) character.
  • You can set the column position for vi to wrap the text by setting the wrap margin variable. For example, to set the warp at column 65 you would enter the command :set wm=15 where 15 is 80 - desired column position.




What Others are Reading Now...

0 Comment to "Working with Vi editor in AIX"

Post a Comment