[lug] Vi notes

Atkinson, Chip CAtkinson at Circadence.com
Fri Nov 10 08:25:41 MST 2000


Greetings,

Here's the text from the handout from yesterday's presentation.


Chip
-------------------------------------------------
How to get by in vi

Why:  Vi is just about universal on UNIX boxes and is available natively 
      or ported to many platforms including UNIX, Linux, VMS, Windows,
      MPE/XL in posix mode.

Where:http://www.vim.org has vim, VIimproved, a great version of vi
      which has multiple levels of undo and other enhancements that
      you shouldn't rely on. :-)

Principles:  Vi is modal meaning that there are separate edit and
      command modes.  The command mode is the "default" mode and can
      be gotten back to by pressing the ESC key.  Pressing ESC
      multiple times has no ill effect.

      Vi is designed to keep your hands on the keyboard and off the
      arrow keys and the mouse.

      Vi is two editors in one -- ex and vi.  You interact with ex by
      typing the : in from the command mode.  

      One of vi's strengths is the combination of numbers and commands
      to repeat the command that many times.

Panic!
     The ! forces it, over writing changes.
     :q!  Drops you out of the editor without saving changes.
     u    Undo changes.  vim has multiple undos, straight vi has one.
     "1 through "9 named buffers with the last 9 lines deleted.
     :e   Reload the file from disk that you are editing.
     ESC  Go back to command mode
     :vi  Back to visual (vi) mode if you are stuck at the : 
     vi -r From the shell prompt, used to recover a file if vi died
          for some reason (e.g. power failure)

Cursor movement:
      h,j,k,l (left, down, up, right (yes, l for right))
      ^f      moves forward a page
      ^b      moves backward a page
      $       moves to the end of the line
      0       (zero) moves to the beginning of the line
      1G      Go to line 1 of the file (first line)
      nG      Go to line n of the file (first line)
      G       Go to the end of the file.
      /pattern search forward for pattern and put the cursor at the 
              begginning of the match.
      ?pattern search backward for pattern and put the cursor at the 
              begginning (left hand side) of the match.
      n       search again forward
      N       search again forward
      %       go to the matching {, [, ], }, (, or )
      ^d      (insert mode) move the cursor back one level of indent.
      ^d      (command mdoe) move the cursor back 1/2 page.
      ma      set a mark a
      'a      jump to mark a

Insert:
     i   insert before the cursor
     a   insert after the cursor
     I   insert at the beginning of text on the line.
     A   insert at the end of the line.
     p   put unnamed buffer contents after/below cursor
     P   put unnamed buffer contents before/above cursor

Simple patterns: 
     searches start with a /
     ^   represents the beginning of a line
     $   represents the end of a line
     [a-z] represents a range of characters from a to z
     [^a-z] represents a character that isn't in the range from a to z
     .   represents any character.
     *   represents zero or more occurances
     +   NOT used in vi expressions 

Search and replace:
     Done from : line (ex command)
     Consist of a range followed by s then the delimiter, a pattern,
     the delimiter, the replacement, the delimiter, and flag(s)

     The g flag means do the replacement on all occurances in a line
     rather than the first one found. 
     The delimiter is customarily the / but need not be.
     The & represents the pattern matched in the search part.

     Examples:
     1,$s/lead/gold/  Change lead into gold throughout the file, 
                      but only once per line.
     1,$s/lead/gold/g  Change lead into gold everywhere in the file.

     1,$s/bars/& Gates/g Put Gates behind bars everywhere in the file.
     35,48s#/usr/local/dbin#/usr/local/sbin#g
                         Change /usr/local/dbin to /usr/local/sbin
                         anywhere on the line between lines 35 and 48
                         (inclusive).

Delete:
     x  delete a character under the cursor
     X  delete a character before the cursor
     dd delete a line
     D  delete to the end of line
     d0 delete to the beginning of the line
     dmotion delete to where ever the motion command takes you.
        e.g. d/hello deletes from the cursor to the word hello.
     :g/^ *$/d deletes all lines that are blanks or only spaces

Change:
     r  replace a character with the next character typed.
     ~  change case of a character.

Copy, cut and paste:
        There is one unnamed buffer and 26 named buffers.  The named
        buffers are named a-z.
        Named buffers are referred to by "buffer, e.g.  "a "d etc.

     yy   yank the line into the unnamed buffer
     "ayy yank the line into buffer a

     p    put the unnamed buffer after the cursor
     P    put the unnamed buffer before the cursor
     "aP  put the buffer a before the cursor

     To cut and paste:
     set a mark (you won't see anything on the screen), move to the
     other end of where you wish to cut/copy.  Then delete/yank the
     contents from the current position to the mark into either a 
     named buffer or the unnamed buffer.
     For example, enter these keystrokes:
     majjjj"by'a 
     to yank (copy) four lines into buffer b.  Here it is broken down.
     ma   -- set mark a
     jjjj -- move down 4 lines
     "b   -- names the buffer to yank into
     'a   -- tells where to yank between.

     A simpler example:
     majjjjy'a 
     to yank (copy) four lines into buffer b.  Here it is broken down.
     ma   -- set mark a
     jjjj -- move down 4 lines
     'a   -- tells where to yank between.


Files:
     vi can manipulate several files either from the command line or
     from the : line.  Here are some handy commands.
     :n       Move to the next file on the command line
     :rewind  Go to the beginning of the list of files and start
              editing that one.
     :e#      Switch between the previous file and the current one.
     :wq      Write and quit (save and exit).
     :w       Write but don't exit.
     :e       Reload current file.

Misc, including handy combinations:
      xp      transpose two characters
      J       join lines.  Check your caps lock if you think you are
              moving and the lines pile up together.
      >%      indent the characters between {'s.  Useful for
              programming.
      !}fmt   from command mode, format the following paragraph.
      :7,35!sort Sorts lines 7 through 35
      .       repeat last edit (insert, delete, change)
      :abbr x hello Abbreviates x to result in hello appearing
              wherever a stand alone x appears.
      :unabbr hello removes the abbreviation.
      :map g somesequence Maps the letter in command mode to a
              sequence of commands. E.g. :map g A; will put a ; after
              a line leaving the editor in insert mode.  Use ^v to
              include an ESC to get out of command mode.  Thus,
              :map g A;^vESC 
      ^g      show the current line and file.

.exrc:
     create a file ~/.exrc with ex command settings in it to customize
     behavior for your preferences.  These commands can also be given
     at the : line. E.g. :se ai

     set ai     Set auto indent on.  Indent to the start of text on
                prev. line.
     set wm=15  Set wrap margin to be 15 characters from rhs.
     set et     Set expand tabs.  When ai is on, et uses spaces rather
                than the tab character.  This is a vim extension.
     set nu     Show line numbers at the lhs 
     set list   Show tabs as ^I and end of line as $
     set showmatch Flash the cursor back to the matching {, [, ], }, (, or )
     set aw     Set autowrite.  When going to the next file, save
                changes in the current one.

------------- END --------




More information about the LUG mailing list