JES: Just Educational Services

vi for REALITY "ED" Line-editor Users
Garrett Hildebrand

The simple guide to vi commands for pick ED experts.

If you are used to using the ROS 'ED' command, this document should serve you as a guide to using vi. It depicts the most commonly used ROS ED commands, and how they might be expressed in vi, the editor.

If you are using any other standard P[a-z]*k "ED" editor, this guide should come close enough to get you started in vi.

If you are using vi, you might find something in here, anyway.

Acknowledgements:

John C. Denney, May 1994, for certain corrections and an addition.

Pete Mastren, 15 July 1993, for the sequence required to do a merge of lines n-m from another file from within vi.

SONNENSCHEIN, "A Guide To vi," for information regarding splitting of lines and pattern-matching examples.

Dick Pick, for making me want to learn another operating system and editor.


ED (ROS)        vi (UNIX)       Comments
--------------- --------------- ----------------------------------------------

***     Cursor Movement, Screen Positioning and Line Positioning        ***

Gn              nG              Goto line 'n'.
n               :n              Alternate goto line 'n'.
         or j   Move down one line.
U1              k               Move up one line.
Nn              n       Move down 'n' lines.
Un              nk              Move up 'n' lines.
T               1G              Top.
B               G               Bottom.
Ln              n       List 'n' lines.
L22 (pagesize)  ^F              List one page (control-F in vi).
U22[L22         ^B              Display the previous page (control-B in vi).
W               ^L              Re-display previous page worth of lines.

***     Locating text           ***

L/sstr/         /sstr/          Locate, then list (or move to) line matching
                                the search criteria /sstr/.
L:sstr:         /^sstr/         Locate, then list (or move to) the line
                                which begins with 'sstr' (col1 search).
A               n               Again or find next (literal character "n").
                N               Find prev. occurence of last /sstr/ search.
Ln-m/sstr/      :n,mg/sstr/     List all occurences of /sstr/ in lines n-m.
                                vi will simply move to the last line in n,m
                                which contains a match for /sstr/.

***     Search Criteria         ***

/sstr/          /sstr/          Simple pattern.
:sstr:          /^sstr/         Match on line beginning with 'sstr'.
                /sstr$/         Match on line ending with 'sstr'.
/str1^str2/     /str1.str2/     ROS ED wildcard is '^' while vi's is '.'.

        Unix vi-only Search Patterns...

                \c              interpret the character 'c' literally.
                .               any single character except newline.
                *               any number of characters. May follow '.' or
                                any literal character, or a class...

        [class] - [] is used to surround a class to match one character...

                [string]        match a char. for any character in string.
                [a-z]           match a char. for any character in range.
                [^string]       match a character for mismatch with all
                                characters in string.
                \<              beginning of word.
                \>              end of word.
                ^               beginning of line (first char in /sstr/).
                $               end of line (usu. last char in /sstr/).

***     Deleting lines          ***

DE              dd              Delete one line.
DEn             ndd             Delete 'n' lines.
DE9999          dG              Delete to end-of-item (end of file).
T[DEn           1Gndd           Delete 'n' lines from the top of the item.
                d1G             Delete from wherever you are to top of item.
DE9999/sstr/    :g/sstr/d       Delete all lines matching search criteria
                                specified in /sstr/
                :v/searchstr/d  Delete all lines NOT(matching) the search
                                criteria specified in /sstr/.
DEn-m           :n,md           Delete lines 'n' through 'm'.
DEn-m/sstr/     :n,mg/sstr/d    Delete lines in the range 'n' through 'm'
                                which match the search criteria specified
                                in /sstr/.
DE10            :.,+9dd         Delete this line and the next nine.

                :.,/string/d    -vi only- Delete all lines from the current
                                position to the first occurence of /string/

***     Replacing Text          ***
                                Notes:
                                str1 is any valid search criterion, as in
                                sstr.
                                str2 is simply a text string or a null
                                string.

R/str1/str2/    :s/str1/str2    Replace first occurence of 'str1' with
                                'str2'.
R4/str1/str2/   :.,+3s/str1/str2        replace str1 with str2 on this
                                and the next 3 lines (for 4, total).
RU/str1/str2/   :s/str1/str2/g  Replace all occurences of 'str1' with
                                'str2' (on the current line only).
R9999/str1/str2 :g/str1/s//str2 Replace first occurence of 'str1' with
                                'str2' on all lines.
                :%s/str1/str2   <- More better!
RU9999/str1/str2 :g/str1/s//str2/g Replace all occurences of 'str1' with
                                'str2' on all lines.
                :%s/str1/str2/g <- More better!
Rn-m/str1/str2/ :n,ms/str1/str2 Replace first occurence of 'str1' with
                                'str2' on lines in the range of 'n'
                                through 'm'.
RUn-m/str1/str2 :n,ms/str1/str2/g       Replace all occurences of 'str1'
                                with 'str2' on lines in the range of 'n'
                                through 'm'.
R               Sstring    Replace current line.

R/str1/str2^    /str1/  Replace str1 through end-of-line with str2.
                Cstr2

R/^^^^/str2/    Rstr2      Overtype characters with str2. ROS ED may
                                require wildcards to be preceeded by a
                                unique match string; vi will require the
                                cursor to be positioned correctly.
X               u               Undo last change (in vi, *all* lines are
                                undone).
AR              &               Repeat last change.

ML              !!tr A-Z a-z    Lowercase characters on the current line.
MU              !!tr a-z A-Z    Uppercase characters on the current line.
MLn             n!!tr A-Z a-z   Lowercase characters on 'n' lines.
MUn             n!!tr a-z A-Z   Uppercase characters on 'n' lines.
                ~               Toggle case of character under cursor.
Note: ROS may have an MCL or MCU command, but it is unknown at this time.

***     Inserting Text          ***
                                In vi,  terminates insert or append
                                mode.

I               o               Enter insert mode after current line.
                O               Enter insert mode before current line.
                i               Enter insert mode at current cursor
                                position.
                I               Enter insert mode at beginning of the
                                current line.
AP/string/      $astring   Append string to line.
                :s/$/string     Another way...
                Astring    <- More better!
AP4/str         :.,+3s/$/str    Append str to this and the next 3 lines.

***     Other stuff             ***

J               J               ROS ED leaves line joined in place after
                                line joined with; vi does a true join of
                                the two lines to create one where there
                                were two.
MEn-m''         :n,mt.          Copy lines 'n' through 'm' after
                                current line.

                :v/searchstr/d  Delete all lines NOT(matching) the
                                search criteria specified in /sstr/.

                :x,ytn          Copy lines 'x' through 'y' after line 'n'.

DEn-m[MEn-m''   :n,mm.          Move lines 'n' through 'm' after current
                                line.
                :x,ymn          Move lines 'x' through 'y' after line 'n'.

                                Multi-part command:
Gk              kG              Goto line 'k';
DEn             ndd             Delete 'n' lines;
Gi              iG              Goto line 'i';
MEn''k          p               Add in deleted lines here.
                                End multi-part command.

sp/sstr/        :s/str1/str1/   Split line at 'str1'.
                R  Split line at current cursor position.

ME9999'itemid'1 :r itemid       Merge in file/item 'itemid' after current
                                line.

MEn-m'itemid'   (see below)     Merge in lines 'n' through 'm' from itemid.

                                vi has no way to merge in particular lines.
                                Here are three methods you may use:

                        Method One Merge (Compliments of pfm)
                                Multi-part command (vi merge n-m'id'):
                G               go to end of file
                :r file         read the file
                ma              mark this position
                /xyz/           locate beginning of section
                mb              mark beginning
                /def/           locate end of section
                me              mark end
                'b              back to beginning
                "ay'e           yank to end into buffer a
                'a              back to top of file
                dG              delete file
                /qrs/           locate context
                "ap             insert contents of buffer a
                                End multi-part command (vi merge).

                        Method Two Merge
                                Multi-part command (vi merge n-m'id'):
                :!vi -R itemid  Escape to shell and vi the other file.
                :n,mw scratchid Write the desired lines to a scratch file.
                :q              Exit from the other file, return to the
                                original.
                :r scratchid    Read in lines from other file to current
                                position in current file.
                                End multi-part command (vi merge).

                        Method Three Merge
:r! awk '{line[NR]=$0} END{for (i=n; i<=m; i++) print line[i]}' itemid
                                - where 'n' is the start line, 'm' is the
                                end line, 'itemid' is the UNIX filename
                                and all other text is typed in exactly as
                                shown. (Garrett's method)

:command        :!command       Perform a TCL or shell command (disabled on
                                ROS ED verb).
                :sh             Go to the UNIX shell, stay until D
                                input.

FI              :wq             File the item and exit.
FS              :w              File the item and don't exit.
F                               no analog in vi.
EX              :q              Exit.
EXO             :q!             Exit, ignoring changes.

***     vi-only stuff           ***

                :set showmode   Do this upon entering vi to cause the mode
                                of operation to be displayed during
                                insert-mode or append-mode operations.
                :set [no]number control display of line numbers.

                ^               Move to start of line.
                $               Move to end of line.
                %               Move to matching curly-brace.
                {               Move to beginning of paragraph.
                }               Move to end of paragraph.
                )               Next sentence.
                (               Previous sentence.

                h               Move left one character.
                l               Move right one character.
                j               Move down one line.
                k               Move up one line.

                .               Repeat most recent change of any kind.
                u               Undo most recent change.
                U               Undo only changes to current line.
                cw              Change only current word.
                c$              Change from cursor to end of line.
                d$              Delete from cursor to end of line.
                D               Delete to end-of-line.

                /sstr/          Search forwards for sstr.
                ?sstr?          Search backwards for sstr.
                n               Next match of sstr.
                N               Previous match of sstr.

                f         Page forward.
                d         Half-page forwared.
                b         Page backwards.
                u         Half-page backward.
                G         display current line, filename, etc.

                w               Next word based on punctuation.
                W               Next word skipping punctuation.
                b               Previous word based on punctuation.
                W               Previoius word skipping punctuation.

                a               Append to right of cursor.
                A               Append to right of current line.
                i               Insert text.
                I               Insert text at beginning of line.
                o               Start new line below in insert mode (open).
                O               As with 'o', but open above.

                r               Replace current character.
                R               Overwrite mode (escape out).
                s               Substitute arbitrary text for current char.
                S               Substitute arbitrary text for current line.
                c               Change text from cursor until .
                C               Change text until end of line.

                x               Delete text one character at-a-time.
                dw              Delete word.
                dd              Delete line.

                :r !cmd         Insert a line containing 'n' lines of output
                                from executing the shell command 'cmd'.

                !fmt -n }       Format the current paragraph (all lines to
                                the next occurence of two contiguous end-of-
                                lines.

***     More on vi pattern matching: examples ***

                /r..d           Matches road, read, rind and others.
                s/./fork        Changes first character in line to string
                                fork.
                :g/^/s//*       Preceed all lines in the file with a '*'.
                :s/$/ EQU */    Append ' EQU *' to the end of the current
                                line.
                /a*             Find a string of zero or more a's.
                /aa*            Find a string of one or more a's.
                /a              Same: any string having at least 1 'a'.
                /^[^a]*$        Find a string having no a's anywhere in
                                the line (not real intuitive).
                /r.*d           Find any string starting with an 'r' and
                                ending with a 'd'.
                /[0-9][0-9]*    Find any number.
                s/[0-9][0-9]*/(&)       Replace any arbitrary number with
                                that same number in parentheses.
                /^!.*\*$        Find the next line beginning with '!' and
                                ending with '*'.

 

Update 04 Mar 1996: Garrett has set up a collecting spot for vi information at http://www.oac.uci.edu/indiv/gdh/vi.html
Version 2, Revision D - 29 Sept 1994
Version 2, Revision C - 03 June 1994
Version 2, Revisions A-B - 04 May 1994
Version 1, 15 July 1993