May 2, 2006

First look at Vim 7

Author: Joe 'Zonker' Brockmeier

The final release of Vim 7 is just around the corner, and it brings a number of new features to the venerable editor, including spell checking, omni completion for several programming and markup languages, tab pages, undo branches, and several other features that are worth upgrading for.

To test Vim, I compiled the 7.0f beta release on Ubuntu Breezy and used it for my day-to-day work for several days. I had been using Vim 6.3, so moving to Vim 7.0 wasn't too drastic. I was relieved to find that I didn't run into any show-stopper bugs or instability while I was working with Vim. It hasn't eaten any files, and none of the new features exhibit major bugs.

Let's take a look at what's new in 7.0.

Spellcheck

Despite the fact that I think spellcheckers are overrated, they come in handy from time to time for spotting typos and errors that you might not catch by eyeballing a file. Spellchecking doesn't prevent people from using "it's" when they mean "its," but having a spellchecker does help catch some potentially embarrassing errors.

Plugins that enable spellcheck for Vim have been available for some time, but this is the first release with on-the-fly spellcheck available as part of Vim itself. As you type, Vim will highlight any word that's misspelled after the word is completed. Vim also highlights words at the beginning of a sentence if they're not capitalized. If you're using GVim, the GUI version of the software, you'll see a red squiggly line under misspelled words rather than solid highlighting.

Vim's spellcheck feature also identifies rare words and words that may be spelled correctly, but which are out of sync with your region. For example, Vim alerts me to the use of colour rather than color, and optimise rather than optimize. Vim also detects some double words, which is a nice touch.

I found the keybindings for spellcheck relatively easy to remember. When spellchecking is turned on, ]s will move to the next misspelled word, and [s will move to the previous one. To add a word to the dictionary, use zg, and use z= to see suggestions. zug will undo an addition to the dictionary, in case you accidentally approve a misspelled word.

Unfortunately, the spellchecking feature and syntax highlighting didn't seem to like one another in Vim, though they seemed to work just fine in GVim. When working with HTML files and syntax highlighting turned on, the spellchecking feature didn't work properly at all. If I turned syntax highlighting off, though, everything was fine. It's not that difficult to turn syntax highlighting off before enabling spellchecking, but it'd be nice if both features worked at the same time.

Undo branches

If you've used Vim for any time, you're probably already familiar with Vim's undo feature -- and the ability to undo (and redo) a large number of operations to a file. Vim's undo feature has always been useful, and now Vim 7.0's undo branches feature is interesting, if not necessarily perfect. Undo branches allow you to return to the file's state by the number of changes or time interval. For example, if you've been editing a file for a few hours and decide that your draft two hours ago was markedly better, you can revert to the earlier state by using :earlier 2h. This will return you to the text state about two hours ago -- with all the changes made in that two hour span removed. If you change your mind, you can reinstate those changes using :later 2h.

While this change is somewhat interesting, it's not as fine-grained as I had hoped, and the :undolist command that shows the "leafs" in the change tree doesn't really provide a great deal of information to work from. It shows the number of changes, and the time, but no text from the changes.

However, the undo branches feature does provide finer-grained control than the normal undo and redo features found in Vim. Instead of using u (undo) and Ctrl-r (redo), you might experiment with g- and g+ to move through the text state the next time you need to undo or redo a change to the file you're working on.

Tabs

Though Vim might be a bit behind the pack on implementing a tabbed interface, it's nice to see this feature making a debut in Vim 7. Vim offers a lot of ways to edit multiple files simultaneously, but I'm used to the tab metaphor, and I'm sure a lot of other users are as well.

In GVim, tabs work pretty much as they do with any other GUI-based text editor. Tabs are placed at the top of the GVim GUI, right above the text area and below the menu and toolbar. You can cycle between tabs using the mouse or command keybinding (gt), and open or close tabs using a right-click, if the tab bar is already open.

With Vim's tab implementation, you can use the :tabdo directive to execute a command -- say, a search and replace -- through all open tabs. So, for example, you could run :tabdo %s/oldvariable/newvariable/g to change the name of a variable through two or more files almost as easily as doing the search and replace on a single file.

The only thing I'm not crazy about with Vim's tab implementation is that I have to learn a new set of keybindings for creating, closing, and cycling through tabs -- or spend the time modifying them. Firefox uses Ctrl-t for a new tab, while the default for Konsole is Alt-Ctrl-n, Bluefish uses Ctrl-n, and so forth, while Vim only offers :tabnew by default. Ctrl-t is already used for jumping to an older entry in the tag stack.

Despite that minor annoyance, I'm glad to see tabs in Vim. Tabs suit my writing and editing style much more than working in split viewports or using Konsole tabs to deal with multiple files.

Code completion and more

The new version of Vim offers a new code completion feature called omni completion that many programmers are likely to find useful. If you're editing, for example, a page in HTML, the omni completion feature will offer possible tags if you press Ctrl-x Ctrl-o. If Vim finds matches, it will pop up a menu that offers all of the possible choices. I tried this a bit with HTML, and the release notes say that C, JavaScript, Python, Ruby, and XML are supported as well.

One small but significant feature in Vim 7 is the addition of a :sort command. Let's say you have a list in Vim that you'd like to sort alphabetically. Simply specify the range and by default Vim will sort the lines by the first character in the line.

Vim now has its own internal grep feature, :vimgrep. This is probably more interesting for Vim users on platforms without grep, but it's useful for Linux and other *nix users as well. :vimgrep lets you search through files (including gzipped and remote files) for a search string, and then load the matching files for editing.

This release also adds the :viusage and :exusage commands, which display a full list of normal and ex mode commands that can be used with Vim. It's a very terse reference, but it's comprehensive and useful to have.

I haven't had the opportunity to test out Vim 7.0's addition of printing support for multi-byte characters, and extended Unicode support.

Upgrade today

I've been using the Vim 7 exclusively since the release of Vim 7.0f beta last month. So far, I've only noticed one minor glitch -- the spellcheck/syntax issue -- and that's a small price to pay for on-the-fly spellchecking, tabs, sorting, and all the other goodies available.

If you're already a Vim user, grab the new release as soon as possible. If you haven't tried Vim before, now's a great time to start. If you're interested in trying out Vim 7, the source code is available from the development page on Vim.org.

Click Here!