Introductory Programming in Python
Using Vim as an Editor

[Course Outline]

What is Vim?

Vim is a text editor standard on nearly all *nix distributions. The only two distributions I know of that don't have Vim standard are gentoo and linux from scratch. Vim stands for 'vi improved' as Vim is based on the original visual (as opposed to line oriented) editor for unix, called 'vi'. Vi, and thus Vim, represent a fundamentally different way of editing files, as compared to the now more common methods employed by editors such as EMACS, Notepad, Kate, and pretty much every other editor out there. The difference lies in the fact that Vim operates in various 'modes', meaning different key strokes do different things at different times. This allows us to use only the standard alphanumeric and punctuation keys to perform all our tasks, because when we are not in insert mode, they can be used for something else. This is important, historically because keyboards used not be standardised beyond the alphanumeric and punctuation keys, and because even today, when remotely connected to another computer via telnet or ssh, there are only limited character sets (primarily employing only alphanumeric and punctuation characters) available on the connection protocol.

It's important to realise that Vim is incredibly powerful, and really huge (in terms of learning quantity). I've been using it for eight years as my primary editor, and still find things I don't know about, and that I actually would start using daily. Never mind all the stuff in the help system I know I don't know about! This document serves only to introduce the very basics of Vim. If you want to know more, in general or about a specific command, and trust me, there's more to know, use Vim's help system.

Starting Vim?

We start Vim in a *nix environment simply by typing vim at the command prompt.

sirlark@hephaestus ~ $ vim

Alternatively, we can choose a file to edit immediately, by specifying it as an argument on the command line

sirlark@hephaestus ~ $ vim myfile.txt

We can in fact specify any number of filenames, and Vim will open them all, although only the first will be visible upon starting Vim. Files that already exist are loaded from disk into a buffer in Vim for editing. Buffers may not be visible, but are still in memory. Changes made to a buffer (changes you make to a file are in fact only made in memory) are only saved to disk when Vim is instructed to write the buffer. Filenames that do not exist create empty buffers, and the file itself is only created upon buffer write.

Command/Normal Mode

Vim starts in 'Command' (aka 'Normal' mode). In this mode keystrokes are not inserted into files, but rather they are used to issue large (or small) scale editing commands, such as cut, paste, delete line, jump to beginning of file, etc... Additionally, entering the character ':' allows us to type more complex commands on a Vim command line. A number of Vim commands use the concept of a motion. Simply put, a motion is a movement of the cursor from one point in the file to another. The motion is considered to be the contents of the file between the cursor's starting point and it's end point. The really important keystrokes you will need are

One of the cooler things about Vim is the ability to specify counts. We can for example specify that one of the above actions indicated by keystroke be performed multiple times by prepending the keystroke with a number. For example if we wanted to insert exactly 72 '*' characters, instead of going into Insert Mode and counting, we simply type 72i*<ESC>. Similarly we can replace the next 7 characters with 'z's (7rz), etc...

Also, in Command Mode, we have the ability to enter more complex commands by using the ':' key. The ':' key present us with a command prompt at the bottom left of the screen (':'), where we can type more complex commands, some of which are

You will have noticed the concept of range in some some of the commands above. In general, we can specify that a particular command should be applied only to a certain range of lines, by entering that range as a pair of line numbers, comma separated as in <from line>,<to line>. '%' reflects the entire file as a range. Type ':help range' for more details, and there are lots more details.

Insert/Replace Mode

Insert Mode acts just as any other editor. What you type gets put into your file. The arrow keys work as normal, delete and backspace work as normal. The only two keys you need to worry about are Escape and Insert. Escape takes you back to Command Mode. Insert will toggle between Insert proper and Replace or Overwrite Mode. When in Insert Mode you will always see the message "-- INSERT --" or "-- REPLACE --" displayed at the bottom left of the screen, depending on whether you are inserting or overwriting.

A useful shortcut when in Insert Mode is CTRL-X,CTRL-f attempts to complete the word under the cursor as if it were a filename. The first available completion is used. You can cycle though available completions by holding down CTRL and pressing 'f' repeatedly. In versions 7 and higher of Vim, a little drop down menu is actually displayed when this happens.

Visual/Selection Mode

From Command mode one can highlight or select text to operate on using the visual ro selection modes, of which there are three. All oF them operate by highlighting from the cursor position when the mode is entered to the current cursor position, and show the message '-- VISUAL --' in the bottom left corner of the screen when we are in them. Exiting the mode by pressing Escape aborts the selection. Essentially the visual modes allow us to specify a range for ':' commands, or keystroke commands from command mode to work on. The three visual modes are

To exit any visual mode without doing anything we simply press Escape. However, we can also issue any of the commands from command mode when in visual mode. The command specified will be executed and then we will be returned to command mode. If that command operates only on entire lines, then all the lines selected, even if only partially selected, will be operated on. Otherwise the interactions of commands and selections are to lengthly to describe, although they tend to be resonably intuitive. All I can say is experiment.

Getting Help

The first place to get help for Vim is in Vim itself, using the :help command. Go though the tutorial at least once, and learn how to navigate the help system. Alternatively, an internet online copy is maintained at http://vimdoc.sf.net. Other places include your local Vim guru, who may charge you in pizza or drinks (caffeinated or alcoholic). Finally, there's a whole collection of Vim gurus who sit on the #vim IRC channel on freenode.org. If you don't know how to use IRC, learn!

The Vim Debugger Plugin

I have written a plugin for Vim (shameless punting here, I know!) that makes Vim act like an IDE for the purposes of debugging python code. It's still under development, but is pretty stable, and should work fine. I use it regularly, without any issues. You can download it from here. Read the quick start file and go from there.

[Course Outline]