A Tiny Vim Tutorial

There are lots of tutorials on the use of Vim out there. You can check www.vim.org to find some of them, or use your preferred search engine. This document contains just enough information to give you the advantages of working with Vim to edit code files. It does not tell you how to become a Vim power user.

The first thing to know about Vim is that its roots go back to the days when there were no mice and everthing entered into computers was typed. So they needed a way to tell when you were typing text into a file and when you were typing commands into Vim. Hence the need for two modes: insert mode and command mode. When you start Vim, you are in command mode (more on that later). To enter text, type the letter i to enter insert mode, and type away. To get back to command mode, press the <Esc> key.

Fortunately, there is a graphical user interface for Vim, and you can do most normal operations, like saving files, using the mouse and menus in the usual way. However, the standard keys for cut and paste (Ctrl-C/Ctrl-V) don’t work in Vim because it uses other keys for those operations. But you can do them using the Edit menu.

When you are in command mode, typing u undoes the most recent editing, and Ctrl-R re-does the most recent undo.

That’s it! With just this much information, you can use Vim to edit your code files and get the advantages of syntax highlighting and the possibility of being much more productive when writing code.

Super Advanced Vim Tutorial

Features Available with my _vimrc File

Vim is highly configurable, customizable, and extendable. In this section I’ll mention some shortcuts you can use in the version of Vim installed in the lab. These features are available because I have replaced the standard Vim configuration file with one that I’ve customized for my own preferences. So if you install Vim on your own computer and want it to work like the ones in the lab, grab a copy of the configuration file from any computer in the lab. It’s C:\Program Files\Vim\_vimrc or, possibly, C:\Program Files\Vim\.vimrc depending on which computer you use. The name _vimrc is normally used on Windows, and .vimrc on Unix, but Vim will use either name, and different computers in the lab may be set up differently. If it’s named .vimrc on the computer you get it from, it probably would be best to rename in to _vimrc on your own computer.

The first thing I did was to set the tab width to 2 and made the editor substitute spaces for tab characters.

Ctrl-S or Ctrl-W will save your work to disk. Ctrl-K “kills” (deletes) the line the cursor is in.

Ctrl-J will reformat a paragraph so lines are less than 76 characters wide. But a “paragraph” consists of all lines that are not separated from each other by a blank line. So you often need to insert tempory blank lines to limit the scope of the Ctrl-J command. Alternatively, you can select lines within the paragraph you want to reformat, either with the mouse or the arrow keys (hold down the Shift key when using the arrow keys), and then type Ctrl-J. If you find 76 is narrower than you like, you can change it to something like 100 characters by entering the command, :set tw=100. If you want to change this setting permanently, you can edit your _vimrc file; the command is one of the first ones in the file.

Other Features

The :retab command will take out any tab characters that have crept into your file and turn them into the proper number of spaces. Tab characters are a nuisance because the View Code option in Firefox considers tab stops to be set every 8 characters, but Dreamweaver and Vim have them set every 2 characters. When viewing/editing your code in these different programs, what looks good in one can appear all jumbled in another.

It’s not an absolute requirement, but there should be no tab characters in the code files you prepare for this course.

You can get help by typing the :he command.

To dismiss the help part of the window, type either :x or :q if you are in command mode, or you can type either Ctrl-X or Ctrl-Q while in either command or insert mode.

Type :x (while in command mode) or Ctrl-X (in either mode) to save your work and exit. Use :q! in command mode to quit Vim without saving any changes you have made to a file, and use Ctrl-Q to quit without saving if you haven’t made any changes.

One last super-super-advanced topic: You can edit multiple files simultaneously. The terminology is that you can have multiple editing buffers. Use the command :n to go to the next buffer, :p to go to the previous one, and Ctrl-^ to switch back and forth between the two most recent ones you have looked at. I’ve added Ctrl-F1 through Ctrl-F8 to take you directly to each of the first 8 buffers you might be working on simultaneously, and those shortcuts work from insert mode as well as command mode.