August 30, 2005

Vim's newest features

Author: Michael M. Murphree

Vim, or "vi improved," is an open source text editor for multiple platforms. This article gives an overview of vim's latest improvements over vi.

You can find this descendant of Vi on your Linux system by typing alias vi and pressing Enter. You'll probably get alias vi='vim'. For a comprehensive look at the differences between vi and vim, you can enter :help differences at the vim command line.

New features include multiple windows, syntax highlighting, multiple levels of undo, and color themes. All of these improvements are made possible by the use of vim plugins.

Plugins are nothing more than scripts that are written for vim. When executed, vim checks /etc/vimrc or ~/.vimrc (if it exists) to get a list of directories that contain plugins. Each .vim file in those directories is then "sourced" -- read in as a list of instructions to vim. These instructions may be carried out automatically, as in the case of syntax highlighting plugins, or may provide you with additional capabilities that you can take advantage of with a pre-defined key combination, for performing a spell-check, for instance. You can also enter :source <path>/<plugin> to manually source a plugin for testing or temporary use.

The ftplugin directory (usually under /usr/share/vim/<version>) contains plugins that are sourced when particular types of file are opened. They contain key mappings and syntax highlighting rules for HTML, Java, C, and other programming languages. For more information about them, type at the vim command line :help filetype.

Some plugins are installed with your Linux distribution; some you can add through your package manager. Check the /usr/share/vim-scripts/plugin directory if it exists on your system. The best source for additional plugins is vim online.

Once you have downloaded a new plugin, there are several ways to make it available to vim.

  • You can copy the plugin to your ~/.vim/plugin directory, which ensures that it will not interfere with other users on your system. The problem with some plugins is that they may use the same keystrokes as those used by others. If this is an issue, you can manually edit the plugin to change the keystrokes. Look for something like <Leader>te mapped to a function. Normally <Leader> refers to the backstroke (\) key, so a mapping of <Leader>te means that you can use \te in command mode to call a function of the plugin.
  • You can copy the plugin to the/etc/vim/plugin directory, or any directory that is sourced in either /etc/vimrc or ~/.vim/vimrc. If you are using a package manager, such as APT or yum, then plugins that are copied to this directory are updated along with the packages that provide them.
  • You can manually source the plugin with the :source <path/plugin> command. If you manually download and copy a plugin to a directory that is not sourced in /etc/vimrc or ~/.vimrc, then you will have to source it before you can use it.

Some plugins can also be called with vim <path/plugin>, or with a shell script from the Linux command line.

Most plugins come with extensive documentation. If, for example, you download the file newplugin.vim, there might also be a corresponding newplugin.txt file. You can copy newplugin.txt to /etc/vim/doc or ~/.vim/doc, then source the help file with :helptags /etc/vim/doc/ or helptags ~/.vim/doc from the vim command line. This allows you to type :help newplugin to access the newplugin.txt helpfile.

Since vim plugins are basically shell scripts, you can create plugins for just about any purpose you can imagine. For more information on writing your own plugins, enter :help write-plugin from the vim command line.

Among the many good plugins available for vim (including an Emacs emulation plugin), here are nine for you to try.

Tutor

Tutor is generally distributed along with vim. You can call it from the command line with vimtutor. This plugin is a good tutorial for the beginning vi user, as it begins with the basic movement keys and continues through editing, command execution, and creation of a personal vimrc file. Even if you're familiar with vim or vi, you'll probably run across something you've forgotten, or never known.

Vimspell

Vimspell is distributed with vim, beginning with version 6.3. It allows you to run your text file through the aspell or ispell spelling checkers, with any resulting errors highlighted within vim. Vimspell works like most other spell-checkers, allowing you to select alternate spellings, skip words, or add words to your personal dictionary.

Explorer

Distributed with vim, Explorer allows vim to display a directory tree when a directory, as opposed to a specific file, is given as an argument to vim. You can then navigate the directory tree within vim and select a file for editing.

VIFM

Although vifm.vim is available as a standalone file manager, you can copy it to your plugin directory to allow both vifm and vimdiff to be used within a normal vim editing session.

UTL

Universal text link allows you to set hyperlinks within your text documents. Hyperlinks can be used to start applications, open related documents, or bookmark parts of your text document.

Calendar

Calendar adds a calendar display to the side or bottom of your vim session. You can attach notes to each date of the calendar.

MRU

The Most Recently Used file list allows you to type :MRU at the command line to display a list of those files most recently edited. This is useful for system administration, especially when you want to see what you've been up to as root.

winmanager

If you find the filetype plugins helpful, then winmanager may also be of real value to you. Entering :WMToggle at the vim command line opens two small panes to the left of your text editing session that display a directory browser and a buffer browser. The result is a small, fast, powerful integrated development environment that provides syntax highlighting and key mappings customized for specific languages. Ctrl-w allows you to use the h, j, k, and l keys to move between windows as you would between characters in vi/vim. You can combine other plugins with winmanager, including a tag explorer, which you can access with the Ctrl-n key combination from the directory explorer window.

Tetris

Yes, Tetris is available for vim, with high score and Traditional or Rotating game play. In Rotating, the columns continuously move left, off the screen, and back to the right again. If for no other reason than to play Tetris on an ASCII console, you should try this out.

In addition to plugins, there are many more features and versions of vim available. Some that are worthwhile are:

Gvim -- Known as the "official" vim GUI session, gvim is excellent for beginning to experienced vim users. Gvim allows you to use a scrollbar, mouse, menus, and cut and paste in a GUI-friendly manner without sacrificing any of the vi/vim keystrokes or conventions.

Cream -- Another X-capable vim session, aimed specifically at the beginning vim user. Cream includes more formatting options and extensive helpfiles.

Vigor -- It's "Clippy" for vim. Best for the vi-phobic.

The growth of vim is staggering. There are currently more than 1,300 plugins available at vim online, some of which are bound to make your life easier or a little more fun. Taking advantage of such a wealth of hard work and talent is more than just good fun; it's good sense.

Michael M. Murphree has been a Linux user for more than a decade. He administers high-performance computational clusters and is certified LPIC-2 by the Linux Professionals Institute.

Click Here!