[lug] vi/vim HOWTO, part 2

PC Drew drewpc at colorado.edu
Mon Apr 3 17:32:56 MDT 2000

I'm repeating the "notes" from the last vi/vim howto, in case you
missed it:

        * 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 2 **

 * More Fundamentals *
Here are some more advanced, yet still fundamental features of vi.  The next
few commands are commands to change/edit portions of a file.
        cw -> change word -- this basically executes a dw (delete word), then
              an i (insert).
        r(char)  -> replace -- this replaces the character under the cursor
              with the specified character.

Movment commands.
        0 -> beginning of the current line.
        $ -> end of the current line.
        b -> beginning -- moves to the beginning of the current word.  If the
             cursor is already at the beginning of the current word, the cursor
             then moves to the beginning of the previous word.
        w -> ? -- moves to the beginning of the next word.  Like the previous
             command, but moves forward instead of backward.
        e -> end -- moves to the end of the current word.  If the cursor is
             already at the end of the current word, the cursor then moves to
             the end of the next word.

Search commands.
        /(characters) -> this searches the file, from the current line, for
                the characters specified.  "/hi" would search for "hi" within
                the file.  If there are more than one instances of the search
                text found, you can use the "n" character to go from one instance
                to the next.
        ?(characters) -> same as above, only searches above the cursor.  For
                example, if the cursor is in the middle of the file, this will
                search from the middle to the top in a backward manner.

Display commands.
        ^f -> page forward (it's actually ctrl-f).
        ^b -> page backward (it's actually ctrl-b).

Other commands.
        ~ -> changes the case of the character under the cursor.  If the cursor
                is on a "p", then it get's changed to a "P".  And if it's on a "P",
                it get's changed to a "p".  Characters other than a-z and A-Z are
                not effected.
        ^v(char) -> this interperates the following character as a literal character
                instead of as it's vi special meaning.  This is similar to useing
                the "\" in UNIX.  For instance, to put the escape key in to a file,
                you'd type "^v<ESC>" and you'd get this: .

 * Advanced Features *
Now that you know some of the fundamentals of vi, you can apply them to the
advanced features to create a very powerful editor.  Once you get comfortable
with vi, you can really fly.

Some of the most powerful commands are in line mode (the mode after typing ":").
The first is the substitute function.  An example of this is:


This may look like some sort of Geek Code to the uninitiated, but it's really quite
logical when you disect it:

        ".,$" -> the line numbers to apply the following command.  The "." refers the
                 current line (i.e. "cd ." goes to the current dir).  The "$" refers
                 to the end of the file.  Therefore, ".,$" means "from the current line
                 to the end of the file...".  You can also specify line numbers like
                 "1,5" or any combination of variables and numbers like "1,$".
        "s"   -> substitute (search and replace)
        "/hi" -> it searches for the text after the first "/" (in this case, "hi").  If
                 another "/" is not present, then it just removes the text in the range
                 specified.  If there is a "/" after the text, see the next field.  Remember,
                 if you really want to search for a "/", escape it in the normal UNIX
                 manor: "\/".
        "/lo" -> replacement text.  Once an instance of the search text is found, it gets
                 replaced with this text.
        "/g"  -> This means do the search/replace for every instance in a line.  So, if
                 you're trying to replace "hi" with "lo" and there's 3 instances of "hi"
                 in a line, adding the "/g" will search/replace all 3.  If you leave this
                 off, then it'll do it to the first "hi" it comes to.

Another line mode function is the delete function.  This command is similar to the above
substitute command, but it deletes lines.  It looks like this:


This is exactly the same as above, so I won't go into what everything means.  The only
difference is the "d" instead of an "s".  This specifies "delete" the lines within ".,$"
instead of substitute.

 * Some Cool Stuff *
        * Less (the replacement for more) seems to be geared toward vi users.  Most of
        the movement commands in less are straight out of vi!

        * If you really like vi and find that it's easy to move around in, tcsh (a
        replacement for csh) has a "bindkey" option that allows the command line
        to behave like an editor.  As far as I know, the only options are "bindkey -v"
        for vi and "bindkey -e" for emacs.  This goes in your .tcshrc file.  Probably the
        best example that I've got, is if you type something on the command line, hit enter,
        and forgot to put "sudo" in front of it (i.e. you get a permission denied error),
        you can scroll up in the command history and type "0" (which takes you to the beginning
        of the line) and then type "i" and type "sudo " then hit enter again.

        * As far as I know, all of the commands that I've explained in this and the previous
        HOWTO work in the traditional vi as well as vim.  Vim, however, has a whole lot more
        features.  To see them all, go to http://www.vim.org/html/ .  It also has syntax
        highlighting for tons of languages.  The syntax highlighting is one of the best
        reasons to use vim over vi.

        * The majority of these commands can be combined with numbers to do multiple times.
        For instance, if you type "8~", then the next 8 characters will have their cases
        changed.  The same for "c4w".  This changes the next 4 words.

 * Conclusion *
If you have more questions, you can feel free to contact me at drewpc at colorado.edu.
However, I do warn you that I don't know nearly enough about vi as I'd like to.  The
commands that I've listed in these howto's are basically the extent of my knowledge.
These commands are the ones that I use day in and day out.  There are some other things
that I have absolutely know idea, like the "map" and logging features.  Finding out about
these things are exercises to the reader.

If someone would like to do a similar HOWTO on emacs, I'd really like it.  Everyone talks
about how emacs is so good, so I'd like to learn how to use it too.

Thanks for read'n, I hope you liked it!

More information about the LUG mailing list