CSCI 344
Using vim


Vim Resources

Online version of Steve Oualline's vim book.


An excellent vi tutorial from Purdue.

The vim homepage (www.vim.org).


Basic Vim Commands

Moving around (while in command mode)

    h left
    j  down
    k up
    l right

    / pattern is search
    n is next
    ? is search back

Inserting text (must be in command mode to start inserting)

    i insert before cursor
    a insert after cursor

    I insert at start of line
    A insert at end of line

    C delete from cursor to end of line, start inserting

The command line editor
When vi was created, editors did not display the text being edited on the screen.  The vi stands for visual. vi was built on top of the line editor ed.  A line editor allows you to edit a single line at a time.

The line editor provides lots of vi's functionality (read file, write file, substitute).

When in command mode, ":" allows you to enter a command that is passed to the line editor.

For example, if you wanted to change all occurrence of foo to bar you could use the s (for substitute) command
: 1,$ s/foo/bar/g
When using the line editor, you must first tell it what lines you want to edit.

In the above example, the "1,$" tells the editor you want to edit all lines 1 to $ ($ stands for the last line)

The "s" stands for substitute

After the first / you put the old string
After the second / you put the new string
the g stands for global (you want to substitute ALL the foo's on a given line to bar.
Three other common commands are
":q" for quit
":w" for write
":wq" for write and then quit
":e filename" to start editing another file


Demonstrate the following

  1. Create a new bash script using cat (the script can be trivial such as "ls" and "date")'
  2. Creating a new bash shell script using vim
  3. Execute these scripts using the source command
  4. Execute these  scripts from the command line by typing its name (think about the PATH and file protection)
  5. Modifying an existing bash shell script using vim
  6. Create a larger file using vim and demonstrate moving around and changing this file.