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:
Update: See the gvim version of vim, easier to install and use than the below. Also, there are nice GUI versions of vim available for Macs.
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
vim -g filename
Here are some powerful mouse operations:
Afterward, if you wish to place that block somewhere else, move the cursor to the latter position and then hit the usual `p' key to paste or click on the paste icon (picture of a jar of paste).
There are also some useful, eye-appealing icons at the top of the vim window.
:spto 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:
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
:qfrom within that subwindow.
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.
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
(which I have aliased to `F' in my ~/.vimrc file).
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
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
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
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
which tells vim not to make the cursor blink (which I find very distracting).
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.
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
before you do the cut-and-paste operation, and then reset by typing
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
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.
where the i and ESC (Escape key) are for the usual vi insert operation.
where the / and CR (Carriage Return, i.e. the Enter key) are for the usual vi search operation.
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
To view a file on a read-only basis, invoke vim with the -v option on the command line.
Make sure your file is writable, and then make sure the autowrite option is set, either by a line
in your ~/.vimrc file or by typing
Then assuming you have a Makefile in the current directory, either type
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.
Vim features quite extensive online help. To get started, simply type
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.
If you want to know what a specific key does, say g, type