Back

File: 102_vi.txt
Last Modified: 2000/01/20 23:23:08
Document Title: Unix Editor - vi notes

Contents

102.1 General --
102.2 Reminders --
102.3 Sample .exrc --

102.1  General --

        vi is an important editor to know in the UNIX enviroment because it is
        always available. The followins notes are reminders for detailed
        information please see the following books:


        Beginners
        --------

        Learning the vi editor
        By Linda Lamb & Arnold Robbins
        Published by O'Reilly
        ISBN 1-56592-426-6

        Advanced
        --------
        The Ultimate Gude to the VI and EX text editors
        By Hewlett Packard
        Published by Addison-Wesley
        ISBN 0-8053-4460


102.2  Reminders --

        Editor Commands

        Esc     Return to Command mode.
        .       Repeat Previous Command.
        Ctl-L   Redraw screen.
        :q!     Quit and abandon changes.
        ZZ      Quit and save changes.
        :w      Save buffer without quitting vi.
        :n      edit next file
        :w fn   Write buffer to a new file.
        :r fn   Read filename and insert.
        vi -r   Recover file after a system crash.

        Insert/Change Commands:

        i       Before cursor.
        I       Before first nonblank character on line.
        a       After cursor.
        A       At end of line.
        o       Open a line next line down.
        O       Open a line next line up.
        rx      Replace single character with x.
        R       Replace characters.
        c(n)    Change characters.

        Cursor movement commands

        space   Right one character position.
        h       Left one character
        j       Down to same position in line below.
        k       Up to same position in line above.
        l       Right one character.
        w       Forward to first letter of next word.
        b       Back to first letter of previous word.
        +       First character of next line.
        -       First character of previous line.
        0       Beginning of current line.
        $       End of current line.
        Return  Forward to beginning of next line.
        (       Back to beginning of current sentence.
        )       Ahead to beginning of next sentence.
        {       Back to beginning of current �.
        }       Ahead to beginning of next �.
        f F x   Find character x in line stop on (repeat ;),
        t T x   Find character x in line stop before (repeat ;),

        File Posistion Commands

        H       Left end of top line on screen.
        M       Left end of middle line on screen.
        L       Left end of lowest line on screen.
        G       Last line in work buffer.
        nG      Move to line number n.
        Ctl-D   Down half screen.
        Ctl-U   Up half screen.
        Ctl-F   Down almost a full screen.
        Ctl-B   Up almost a full screen.
        Ctl-E   Scroll down one line at a time.
        Ctl-Y   Scroll up one line at a time.
        z Rtn   Move current line to top of screen.
        z       Move current line to center of screen.
        z-      Move current line to bottom of screen.
        /pat    Find pat (n repeats N reverses).
        ?pat    Previous line matching pat.

        Delete Commands:

        x       Character at cursor.
        X       Character before cursor.
        (n)dd   Current line.
        d(n)w   Delete to end of word.
        d(n)b   Delete to beginning of word.
        d(n)0   Delete to end of line.
        d(n)$   Delete to beginning of line.
        d(n))   Delete to end of sentance.
        d(n)(   Delete to begining of sentance.
        d(n)}   Delete to end of paragraph.
        d(n){   Delete to beginning of paragraph.
        d(n)G   Delete to end of file.
        d(n)1G  Delete to begining of file.
        u       Undo a command.
        U       Undo an undo command.

        Buffers and Marks

        Y       Yank to buffer 
        yy      Yank to buffer 
        Y'x     Yank from current line to mark x
        "xY     Yank to buffer x .
        P       Put from buffer 
        pp      Put from buffer 
        "xP     Put from buffer x 
        mx      Mark posistion with letter x.
        `x      Goto mark x.
        'x      Goto first of line with mark x.

        Set Behavior

        :set ai         Turn on autoindent
        :set noai       Turn off autoindent
        :set nu         Turn on linenumbers
        :set nonu       Turn off linenumbers
        :set sw=n       Set swiftwidth = to n
        :set            show settings

        ex commands

                        form of commands:  line address command parametes
                                           (line address is a single line
                                           or a range seperated by a comma)
        1               first line in file
        $               last line in file
        .               current line in file
        n               nth line in file
        .-n             nth line before current line
        .+n             nth line after current line
        %               all lines in file (1,$)
        'x              line with marker x
        /  /            first line in forward search matching pattern
        ?  ?            first line in backward search matching pattern
        
        n,ns/ / /       substitue second pattern for first on lines n - n
        %s/ / /         substitue second pattern for first in whole doc
        %s/ / /c        substitue and confirm
        %s/ / /g        substitue multiple times per line
        
        %!command       run all lines through unix command
        n,n!command     run lines n-n through unix command
        .,'x!command    run lines lines current-marker x through unix command
        %!expand        expand tabs to spaces
        %!expand -a     convert spaces to tabs 

        :ab xxx xxxxxx  abbreviate xxx to be expanded to xxxxxx
        :uab xxx        turn abbreviation off for xxx

        :map            show mapped keys
        :map x xxxxx    map char x to xxxxxx

        @x              exec named buffer x in a shell

        :list           show control character on current line



102.3  Sample .exrc --

        A file named .exrc located in the users home director will be executed
        when vi starts. The example below can be found in
        /share/local/lib/skel/vi/.exrc

        map # :?^$?+1,/^$/-1s/^/#/^M/^$^M/.^M
        map % :?^$?+1,/^$/-1s/^#//^M/^$^M/.^M
        map * :?^$?+1,/^$/-1!hang^M/^$^M/.^M
        map @ :r !date^MkJ
        map [ :?^$?+1,/^$/-1!hang -w^M/^$^M/.^M
        map ] :?^$?+1,/^$/-1!hang -wn^M/^$^M/.^M
        map { :?^$?+1,/^$/-1!hang -bw^M/^$^M/.^M
        map } :?^$?+1,/^$/-1!hang -bwn^M/^$^M/.^M


        Notes:  a group is defined any number of lines seperated by blank
        line above and below

                # will prepend a # to all lines in current group
                  and addvance to cursor to next group

                % will remove a leading # from all lines in current group
                  and addvance to cursor to next group

                * will align all lines in current group with the first
                  line and advance cursor to next group

                @ appends date to end of current line

                [ will wrap all lines in current, indent same as first  
                  line, and advance cursor to next group

                ] same as above except we reduce right margin by same as left

                { will wrap all lines in current, indent same as first line,
                  (except first word will "hang" outside to left of paragrpah)
                  and advance cursor to next group

                } same as above except we reduce right margin by same as left
                
                The macros fail if used on the last group in a file and there
                is not an empty line after the group.


        When making your own macro note that:

        - you can't see the file correctly if you "cat" it. The imbeded
          ^M will overwrite part of the line

        - You must imbed a real CR not a ^ and M.

        - how the map works:

          map # :?^$?+1,/^$/-1s/^/#/^M/^$^M/.^M
          map                                           map command
              #                                         key to map
                :                                       : key to get to ex
                 ?^$?+1                                 1st address
                                                          search backwards
                                                          for empty line 
                                                          go forward 1 line
                       ,
                        /^$/-1                          2nd address
                                                          search forward 
                                                          for empty line
                                                          go backward 1 line
                              s                         substitue command
                               /^/                      find beginning of line
                                 /#/                    add #
                                    ^M                  need CR to finish line
                                      /^$^M/            find next empty line
                                            .^M         move forward one line



Back

Hosted by www.Geocities.ws

1