[lug] vi/vim HOWTO (it's kinda long!)

PC Drew drewpc at colorado.edu
Thu Feb 10 17:25:51 MST 2000


Before I go into more detail about vi, I'd like to make a few points
about editors:

        * If you're setting up a machine that will be used by multiple
          people, _always_ install both emacs and vim (you can install
          xemacs, vim, pico, joe, jed, etc. if you're really nice).  At
          the very least, however, make sure that both vi and emacs are
          on the machine.

        * I'm sure emacs is a very good editor, but I've never used it.

        * From this point forward, I will refer to both emacs and xemacs
          as "emacs" and both vi and vim as "vi" unless specifically stated.

        * Now, having said those things, there is a _war_ going on
          about which editor (primarily vi or emacs) is better.  It is my
          belief that which editor you use has a lot to do with your
          personality type.

        * There are a _ton_ of commands for most editors, vi included.
          The commands listed here are just the tip of the iceberg.
        
        * Some of the following will be fact (command x does this), and others
          will be my own little tidbits.  If you don't like what I've said,
          stop reading.  That's my disclaimer.
          
Okay, 'nuff said about that.  I'm going to break this up into two sections.
Section 1 will include commands that I think _everyone_ should know.  Section
2 will include more indepth commands for those who are interested in actually
using vi for all of their text editing needs.

** Section 1 **

 * History *
Vi started out as the line editor called "ex".  Ex/vi was developed
at UC Berkeley by Bill Joy for an early BSD release.  It is currently
included in every UNIX distribution.

http://www.techfak.uni-bielefeld.de/~joern/jargon/vi.HTML

 * Why learn even a little bit about vi? *
Well, it's not a bad idea to learn basic editing commands considering
it's installed on _every_ UNIX distribution.  Even if you're an emacs person,
it's good to know how to get around in vi in case you come to a box (i.e.
Solaris) that doesn't have emacs installed as a default.

 * Fundamentals *
Vi was written in such a way that for the majority of commands,
you don't have to move you fingers off of the keyboard.

To learn vi you must understand the following: there are three modes
in vi.

        1. Command Mode -> normal and initial state, other modes return to this
        state.
        2. Input Mode -> once in this mode, you can type freely.
        3. Line Mode -> waiting for input after certain commands.

You'll mostly use the command and input modes.  Now on to the nitty gritty.
When you open a file using vi (i.e. "vi file.c"), you will start out in command
mode. To start typing, you have to type any one of the following keys:

        i -> insert -- allows you to start typing where the cursor is currently.
             When you first open the file, the cursor will be in the upper left
             most corner.
        a -> append -- instead of typing where the cursor is currently, it moves
             over one character then allows you to start typing.  This is _very_
             useful when you want to add more characters to the end of a line.
        o -> open -- insert a new line, then go into insert mode.

After typing the things you need to type, you'll want to save the file (or not
save the file).  In order to do that, you need to be in command mode.  To get
out of input mode and in to command mode, all you have to do is hit the escape
(ESC) key.

The following key combinations will help you get out of vi (and do some other
stuff).  When you see a ":", that actually means type a colon.  Typing a ":"
puts you into line mode.  When you press the "enter" key and after whatever
you told it to do, it will revert back to command mode (unless you quit the
application).

        :w   -> write -- saves the file.
        :wq  -> write then quit -- saves the file the quits vi.
        :q   -> quit -- quit vi without saving the file.
        :w!  -> force a write -- if you're the owner a file, but only have read
                permission on the file, this will bypass that and actually save
                the file.
        :wq! -> force a write then quit -- same as above, then quits vi.
        :q!  -> force a quit -- quit without saving the file.  If you just type ":q",
                and you've made changes, you'll get an error saying that you've
                changed the file and you must either write it or use ":q!".
        :e!  -> re-edit the file -- dump any changes since the last save and re-edit
                the current file.

Next, moving the cursor around in a file.  All of these commands are to be
entered in command mode. Vi newbies beware - you can use the h, j, k, and l
keys to move around, but it takes a little getting used to!

        (arrow keys) -> move you around in the direction that they point.
        h -> left -- move the cursor left (like left arrow).
        j -> down -- move the cursor down (like down arrow).
        k -> up -- move the cursor up (like up arrow).
        l -> right -- move the cursor right (like right arrow).
        (number)G -> goto -- that's right, a number followed directly by
                     the letter "G".  That takes you to the specified line
                     number.  If you just type "G", you'll go to the end of
                     the file.  To go to the beginning of the file, type "1G".
        0 -> beginning of line -- takes you to the beginning of the current line.
        $ -> end of line -- takes you to the end of the current line.

Cut, Copy, and Paste.  These commands are really powerful!  When in command mode,
you type any of the following key combinations.

        yy -> copy (yank) -- copies the current line.
        y(number)y -> Copy (yank) -- copies the (number) lines.
        dd -> cut (delete) -- cuts the current line.
        d(number)d -> Cut (delete) -- cuts the (number) lines.
        dw -> delete word -- cuts the word pointed to by the cursor.
        x -> delete character -- deletes the character pointed to by the cursor.
        D -> delete to the end of line -- cuts everything from the cursor to the end
             of the line.
        p -> paste (or put) -- pastes what is in the "clipboard".  When used after
             a yy or a dd, it inserts the copied lines after the line where the cursor
             is.  When used with "dw" or "D", it pastes the characters starting where the
             cursor is pointing.

Here are a list of other commands that should be executed in command mode.

        . -> repeate last command -- it repeats whatever you just did.  If you
             typed "dd" to delete 1 line, it will execute another "dd".
        u -> undo -- does just that, undoes your _last_ command.  I'm almost positive
             that this is a bug in the original vi, but it is fixed in most vi clones
             (i.e. vim).  In vim, you can undo multiple commands.

** Section 2 **

This will appear (magically!) in another email because I have to attend
class now.  T'll then, have fun playing around with vi.

-- 
PC Drew

"To understand recursion, we must first understand recursion."






More information about the LUG mailing list