Tuesday, April 3, 2007

VIM

The VIM (VI iMproved) editor is available for most platforms and is available as a native Window's GUI application as the program gvim. The VIM editor has many customization features which are controlled by the vimrc configuration file.

VIMRC
The vimrc file may exist in several different locations and different names depending on your platform and installation. You can access vim's online help for additional information with the command :h vimrc. The default vimrc file does some nice things like setting up syntax highlighting so it is recommended that you either modify the default file or copy it for a user specific customizations. These are the settings that I use.

  • ai or autoindent This option automatically indents to the level of the previous line.
  • et or expandtab Insert an appropriate number of spaces instead of tabs.
  • sw or shiftwidth The number of spaces to use for each step of auto indent. Set this option to 4.
  • ts or tabstop The number of spaces a tab character counts for. Set this option to 4.
  • sts or softabstop The number of spaces a tab counts for while editing. Set this option to 4.
  • fdm or foldmethod This option enables code folding in the editor. I set this option to indent.

I set the last line of my vimrc file to:
set ai et sw=4 ts=4 sts=4 fdm=indent


Splitting Windows
  • :sp to split the current window into two views.
  • C-w C-w jumps between the windows.
  • C-w+ or C-w- to increase or decrease the view size.
  • C-wc or :q will close the selected view.

Using Tags
To enable tags do :set tag=.
C-] or C-W] follows tag for item under cursor (existing or split window).
C-T returns to your previous spot.
Searching tags :tag / or :tag for completion
:ts will list multiple matches.

Code Folding
The indent fold method works very well for source code if one uses good and consistent indentation. In this case vim will fold the code based upon the level of indentation. For example:


public void preInit(URL url) {
+--- 46 lines: or the default if no config files was specified.------- } // end preInit

public void init()
{
+--- 60 lines: preInit(getCodeBase());-------------------------------- } // end init

If one wants to use folding but not for source code then the fold method can be set to marker (fdm=marker). A marker will define a folding level for a hierarchy of
folds. A marker is the string { { { # where the # is a numeral defining the fold level. For example:

VIM {{{1
* To insert tabs when in expandtab (et) mode use C-V

windows {{{2 * :[#]sp to split the current window into two views


From command mode the following basic commands can be used to control the folding.
  • zR Open all folds
  • zo Open one fold under the cursor.
  • zO Open all folds under the cursor recursively.
  • zc Close one fold under the cursor.
  • zC Close all folds under the cursor recursively.

Misc Notes
  • To insert tabs when in expandtab (et) mode use {{C-V}}