Professor Norman Matloff's Vim Web Page

Check these first!

The documentation on vim is a huge as the Manhattan phone directory. Here is some quick information on some of the features:

Compiling/installing the package:

If you are working on a Linux or other UNIX system, you almost certainly already have vim installed. (Though you may not have a version that allows the mouse-capable GUI.) If not, here is how you can install it. First, download the package from The vim home page. You will need both the source package and the runtime package.

Then to a directory where you will do your work, and unpack both packages in the same directory. You should now have runtime/ and src/ as subdirectories of this directory.

Then enter the src directory, and go through the usual "configure; make; make install" sequence. The nicest version, with GTK icons, will be generated if you use the --enable-gui=gtk option to configure.

Mouse operations:

If you are using vim in an X11 environment, the mouse can be used very effectively. To invoke vim this way, first make sure that you have set the VIMRUNTIME environment variable (in the C shell), e.g.

setenv VIMRUNTIME /usr/local/share/vim/vim55

Then type

vim -g filename

Here are some powerful mouse operations:

There are also some useful, eye-appealing icons at the top of the vim window.

Subwindows:

Type

   :sp
to split the current vim image into two subwindows. (You can use this even if you are running vim from a text-only, nongraphics terminal.) This way you get to look at two parts of the file at the same time. For example, if you are editing a program source file, you can look at a call to a function in one subwindow and the function declaration in the other one. Note that you can move around independently within the various windows; for example, you can do a `/' search in one subwindow while the other subwindow cursor positions remain fixed.

When you create the two subwindows, you can make them of unequal size. For example:

   :8sp

will make the first subwindow of size only 8 lines.

To move back and forth from one subwindow to the other, type

   ^w^w
`^' denoting the Control key. (I use this often, so I have it aliased to `,' in my ~/.vimrc startup file; see below.)

To delete a subwindow, type

   :q
from within that subwindow.

Infinite undo/redo:

In ordinary vi, you hit the u key to undo only the most recent change. In vim, you can keep hitting u, undoing many changes; if you hit the u key n times, you cancel the last n changes you've made to the file. This is VERY useful.

If you undo a change but then decide you want to make the change anyway---in other words, you want to ``undo the undo''---hit control-r. This will undo the most recent undo. Hitting control-r n times undoes the n most recent undo's.

Paragraph formatting:

I use this constantly. It makes a paragraph of text look nicer, by making all the lines about equal length. This is especially nice when you are writing e-mail; if you send nicely-formatted paragraphs, the recipient will find the reading more pleasant.

To invoke this operation, type

{gq}

(which I have aliased to `F' in my ~/.vimrc file).

Startup (initialization) files:

Upon startup vim will first source ~/.vimrc; if that file is not present, it will source ~/.exrc, the standard name for vi startup files.

Since you might be using more than one version of vi, I suggest that you put your "general" vi startup information in ~/.exrc and then place your vim-specific ones in ~/.vimrc. Near the beginning of the ~/.vimrc place a line

source ~/.exrc

in order to have vim uses the startup information from ~/.exrc.

Upon startup, your system may also first source a global vim file, such as /usr/share/vim/vimrc, which vim automatically sources on startup. If you don't like some of the settings it makes, you can either undo them in your own vimrc, or if you have control of the global file, remove the offending lines in that file. For example, on my Linux system, I added the line

set nohlsearch
set noincsearch

to the vimrc file in my home directory, because I hated that vim was highlighting my / searches and doing them incrementally. I also added a line

set nocindent 

so that vim would not impose its weird indendting on my .c files. More generally, I turned off Vim's using its own language-specific indendting policy, with the command

filetype plugin indent off

I also did

set shiftwidth=3

for the same reason. This gave me 3-space indenting after certain lines.

If you are using the GUI version of vim, you should also make a startup file for it, at ~/.gvimrc In mine, for instance, I have the line

set gcr=a:blinkon0

which tells vim not to make the cursor blink (which I find very distracting).

Editing multiple buffers:

A typical programming project involves source code distributed across various files. During the writing and debugging process, you will usually want to be editing all the files, or at least several of them, at once, each in its own vim buffer.

Another way in which you will have multiple buffers is that a new one is created each time you use vim's online help command.

This is certainly possible with vim and most other modern editors, either by specifying them all at the time you invoke vim from the command line, or for example in GUI mode by clicking the leftmost icon, which shows a picture of an office file being opened.

To move from buffer to buffer in GUI mode, click on Buffers and then select the desired buffer, or in non-GUI mode, use the :buffers and :buffer commands.

To delete a buffer in GUI mode, click on Buffers then on Delete.

Cutting and pasting into vim from an external app:

Suppose you want to copy some text from another application, say a Web browser, into your vim buffer. This will result in autoindent, which you probably don't want. So, turn that off by typing

:set paste

before you do the cut-and-paste operation, and then reset by typing

:set nopaste

Working with non-ASCII bytes:

Suppose you are editing a file which has bytes outside the ASCII range of 0-127, or wish to insert such bytes. Vim allows you access to such bytes.

To enable this, you may need to type

:set bin

Bytes outside the range 0-127 are specified as ^vun, where ^v means control-v and n is a two- or three-digit decimal number.

Examples:

Note carefully that by default vim will insert an end-of-line character at the end of the file, which you may not want to have. If you need to NOT have it, type

:set noeol

Read-only viewing of a file:

To view a file on a read-only basis, invoke vim with the -v option on the command line.

Compiling from within vim:

Make sure your file is writable, and then make sure the autowrite option is set, either by a line

set autowrite

in your ~/.vimrc file or by typing

:set autowrite

Then assuming you have a Makefile in the current directory, either type

:make

or if you are in GUI mode, then click on the hammer icon.

If there are any compilation errors, vim will report them and after you hit Return, vim will take you to the line where the first error occurs.



Online help:

Vim features quite extensive online help. To get started, simply type

:help

You then can move around the help file using regular vi cursor-movement operations. If you see a topic there which you want to read about, e.g.

|X_re|

then type

:help X_re

If you want to know what a specific key does, say g, type

:help g