Computing and Network Services Illinois Institute of Technology

V. Text Editors

vi Text Editor

vi is a screen oriented text editor which can be used
with a variety of terminals.    It is a standard editor for
the IRIX operating system for creating ASCII text files.
Everything in vi is case sensitive.

To call vi type: vi (options) filename

If the file already exists, the first page of the file will be displayed on the screen, and a final line will be desplayed for state and commands. If the file doesn't exist, then it will be created . Some useful options are:

        +n   -the first page displayed starts with line n of the
              file (n is a decimal number)
        +    -the first page displayed contains the end of the
              file
        +/<text> -the page displayed contains the first line of
                  the file which contains the string <text>
        -r  -vi tries to recover working buffers for the file
             given as parameter, in case of interruption of the
             editing session
        -R  -the file is opened only for viewing
        -wn -on the screen will be displayed the first n lines

Examples:

vi +100 p1.c
From file p1.c, page which starts with line 100 will be displayed on the screen

vi +/SIGALRM p1.c
The first line displayed on the screen will be that which contains the first occurrence of the string SIGLARM.

Any file can be displayed, without modifying it, by opening the editing session in read-only mode:

view file_name

B. vi Modes

vi has two modes, one for adding text to a file and the other to manipulate text or navigate in the file. The two keyboards modes are 1) text input mode and command mode.

Text Input Mode - the text entered from the keyboard is inserted in the file and the cursor can be moved.

Command mode - a colon ":" switches to the bottom of the display where various commands may be entered

Text Input Mode is entered by typing:

        a - which allows insertion of text after the current
            position of the cursor
        i - insertion before the current position of the cursor
        A - insertion after the last character of the line
        I - insertion at the begining of the current line
        o - insertion of new text after the current line
        O - insertion of new text before the current line

and exited with the <ESC> key.

To exit vi there are two options:

  1. To exit without saving the results of the editing process type :q!<ENTER>
  2. To exit with saving the results of editing process type :ww<ENTER> or type ZZ

C. vi Commands General Format

The majority of the commands are given from Command Mode, preceded by ':'. Other commands can be given from inside Text Input Mode, but NOT during any kind of insertion of text.

the general fromat of the vi commands is:

[number]operator[number]argument

Example

        dd      - erase the current line
        d$      - erase, in the current line, all the characters
                  from the cursor to the end (except for
                  the NewLine character)
        5dd     - erase 5 lines begining with the current line

        In these commands the 'd' is the delete command, the next
        character shows where the command is applied and 5, for
        example, is a repetition factor.

D. vi Commands

In Command Mode, use the following commands for moving the cursor:

        j or "down arrow"       - move down to the next line
        k or "up arrow"         - move up to the next line
        l or "left arrow"       - move forward by letter
        h or "right arrow"      - move backward by letter
        F<character>            - move backward to the specified
                                  character
        f<character>            - move forward to the specified
                                  character
        w                       - jump to the beginning of the
                                  next word
        b                       - jump to the beginning of the
                                  previous word
        O                       - jump to the beginning of the
                                  current line
        <ENTER>                 - jump to the begining of the
                                  next line
        -                       - jump to the begining of the
                                  previous line
        $                       - jump to the end of the current
                                  line
        nG                      - jump to the begining of n-th
                                  line
        G                       - jump to the beginnig of the
                                  last line of the file
        H                       - move the cursor to beginning of
                                  the first line

E. Commads for Scrolling the Screen

        CTRL-F                  - page-down, scrolling forward
        CTRL-B                  - page-up, scrolling backward
        CTRL-U                  - scrolling up with half-screen
        CTRL-D                  - scrolling down half-screen
        CTRL-L                  - re-display current screen.
                                This command could be useful
                                after many operations of string
                                replacing.  In that case
                                on the screen are displayed the
                                modified lines  or other
                                infomation which reduces the
                                editing zone of the file.

F. Commands for Deleting

        x                      - erase character from the current
                                 position
        X                       - erase character at the left of
                                  cursor
        dW                      - erase current word
        dd                      - erase current line
        d$                      - erase from the cursor to the
                                  end of the line
        D                       - erase from the cursor to the
                                  end of the line
        d0                      - erase from the cursor to the
                                  beginning of the line
        CTRL-U                  - erase all the text inserted
                                  from last entering in Text
                                  Input Mode until now

G. Commands for Text Replacement

r<character>            - replace the current character with the
                          character specified by the command
R                       - replacing starts with the current
                          character and ends at <Esc>
s                       - substitutes the specified number of
                          characters for the current character,
                          for example, 2sda replaces the current
                          character  with 'da'
S                       - delete the specified number of lines
                                  (1 if nothing is specified) and
                                  replace them with the entered
                                  text)

Searching Commands

 /<text><ENTER>     - search forward for the string specified by
                      <text> starting from the current cursor
                      position

/<text>/-n<ENTER> - search forward for the string specified by

                      <text> starting from n lines before the
                      current cursor position

?<text><Enter>      - search backward for the string specified by
                      <text> starting from the current cursor
                      position

n                   - repeat the last search command

Note:
To include in the searched string special characters like $, ~,^,[,],\,.,*, you must prefix them with the backslash character.

H. Moving Text Blocks (Cut and Paste)

One way to move a text block in vi is the following:

#dd - erase the number (#) of lines contained in block; during the deletion the lines are put in a special buffer named "paste buffer". Next, move the cursor to the line where you want to put the block, then press "p" to insert the "paste buffer" contents after the line on which the cursor is positioned.

       For multiple insertions of a single line,
        - move the cursor to the beginning of the line,
        - copy the entire line into buffer 'a' with command
                ayy
        - move the cursor the line after which you want to make
        the copy and type the command ap.   Now, the line is
        copied but remains in buffer a and can be inserted
        in many other places.

I. Cancelling Commands

The command undo (u) cancels the effect of the last command execution.

J. Repeating Commands

To repeat one command use character '.'. Moving the cursor with arrows will not affect the last entered command.

K. Command Line Commands

Commands changing the file may be entered at the bottomof -the-display command line rather than with the cursor located in the text. When you press : vi remembers the current line, but places the cursor at the bottom of the display.

  
  • For text substitution in the current line, type :s/old_text/new_text/g this command replaces all occurrences of text_old with text_new in current the line. (If the g option is missing then only the first occurrence is replaced.)
  • Text substitution in a specific part of the file :n1,n2s/old_text/new_text/g this command replaces all occurrences of text_old with text_new in all lines from n1 to n2 of the file. :1,$s/old_text/new_text/g since $ means the last line of the file, this replaces old_text with new_text in the whole file.
  • Search and Replace commands :g/old_text/s//new_text/ search for old_text in the file and replace the first occurrence with new_text. For example, :g/delay/s//sleep/gp will search for all the occurrences (the last g option) and replace 'delay' with 'sleep', in all the lines of the file (first g).
  • Read a file during editing :r file_name reads in the file with the specified name and inserts it at the current cursor position.
  • Saving the file without exiting :w
  • Saving in a different file (Save As ...) :w file_name
  • To save only selected lines: :10,15w file_name saves all the lines between 10 and 15 in the specified file
  • Exiting from vi :q - end the editing session :q! - exiting without saving current edited file :wq - exit with saving the edited file ZZ - exit with saving the edited file
  • Backto Mail - PINE.
    Forwardto Edit - PICO.


    Send comments to: helpdesk@charlie.acc.iit.edu