Sunday, 17 September 2006

A visual walk through of a couple of the new features in Vim 7.0

A very prominent personality once said, and I quote :
"Sometimes, people ask me if it is a sin in the church of Emacs to use the editor Vi. It is true that Vi-Vi-Vi is the editor of the beast...."
Just for once, I wouldn't mind siding with the beast if that is what it takes to use Vi. The modern avatar of Vi is Vim - the free editor created by Bram Moolenaar. Riding from strength to strength, this editor in its 7th version is a powerhouse as far as an editor is concerned. When ever I use Vim (or GVim for that matter), it gives me the impression of the Beauty and the Beast. It is very beautiful to look at - if you are like me who finds beauty in software , and it is also as powerful as a beast. I use this editor exclusively for all my editing needs that, when I use any other editor, I inadvertently press escape key. That should give you an idea of how ingrained the use of this editor has become in my computing life.

Vim 7.0 - the latest version has a slew of new features built into it. Some of them which I am aware of are as follows:

On the fly spell checking
Vim 7.0 has an on the fly spell checker built into it similar in lines to that found in Microsoft Word. By default, this feature is turned off. But by navigating to Tools -> Spelling -> "Spell check on", you can make Vim display all the mis-spelled words in your document. It does this by highlighting them with red coloured wriggly lines. And all it takes to correct the misspelled words is to move the caret to the highlighted word and while in "Vi Command mode", press 'z=' and Vim will show a list of words closest in relation to the misspelled words and the user can choose from them.

Fig: On the fly spell checking

Suppose, you want GVim to recognise the word GVim as a valid word. It is possible to tell the spell checker that GVim is a good word by moving the cursor on the GVim word and pressing 'zg' in command mode. On a similar vein, it is possible to tell Vim spell checker that a word is wrong by pressing 'zw'. Vim stores the good/bad words that the user recognises in a file associated with the variable spellfile. If the file is empty or not created, then Vim will create one automatically and store the words in this file for future use.

Other spelling related commands
:set spell - Turns on the spell checking
:set nospell - Turns off the spell checking (can be achieved using GUI too).

:]s - Move to the next mis-spelled word in the document.
:[s - Same as above command but searches backwards.
z= - Shows a list of close matches to the mis-spelled word from which the user can pick the correct one.

Bracket highlighting
This feature is most useful for programmers than for ordinary users. Vim will automatically highlight the corresponding closing bracket when the caret is moved over any bracket. This helps in keeping track of the blocks of code and is especially useful when writing code which make use of multiple layers of brackets. Ofcourse this feature could take up some memory. But you can use the ':NoMatchParen' command to disable this feature.

Fig: Bracket highlighting

Omni completion
It is a smart kind of completion like the intellisense. It is most useful for people who write code. For example, if I am writing HTML code and I have saved the file with the extension .html, then Vim will automatically load the HTML tag file and when ever I want Vim to auto complete the HTML code, while in Insert mode, I press the key combination [Ctrl+x] [Ctrl+o] and Vim will smartly guess the correct keyword and insert it. If there is any ambiguity, then Vim will show the possible completions in a pop up window. This feature is presently available for 8 languages which include C, (X)HTML with CSS, JavaScript, PHP, Python, Ruby, SQL and XML. It is also possible to add custom omni completion scripts.

Fig: Omni completion - a boon to the programmer.

Open files in tabs
One of the most useful user interface is the tabs. Support for tabs in applications have been well received by the ordinary users that most web browsers and editors now support opening pages in tabs. Following this trend, Vim has also incorporated tab support in its latest version. And like all things related to Vim, it is entirely possible to open new files in tabs and manage tabs using the commands entered in the Vim Command mode.

Fig: Open files in tabs.

For example, while editing a file in Vim, I wish to open another file in a new tab. I can move to Command Mode and enter the command as follows:
:tabe /home/ravi/Desktop/myotherfile.txt
... and Vim will open the file "myotherfile.txt" in a new tab.

A few other tab manipulation commands are as follows:

:tabs - View a list of tabs that are open with the file names. Use the command ':tabs' and Vim will display a list of all the files in the tabs. The current window is shown by a ">" and a "+" is shown for any modifiable buffers.

:tabc - Close the current tab.

:tabnew - Open a new file for editing in a separate tab.

:tab split - Open the file in the current buffer in a new tab page.

:tabn - Switching to the next tab page.

:tabp - Switch to the previous tab page.

:tabr[ewind] - Go to the first tab page. You get the same effect when you use the :tabf[irst] command.

:tabo - Close all other tab pages.

Undo Branches
One of the things I really like about Vim is the use of the key 'u' to undo any changes. When ever I make a series of mistakes while editing a document, I just move to command mode in Vim and press the key 'u' a series of times to move to a point prior to the mistakes.

Fig: A list of undo levels with the time

In Vim 7.0, a new feature has been included which allows a user to jump back or forward to any point of editing. For example, I am editing a document and after a couple of minutes (say 10 min), I realise that I have made a mistake. I can easily take the document to a point 10 minutes back by using the command :
:earlier 10m
Or for that matter, move to a point 5 seconds ahead by using the command:
:later 5s
You can use the command :undolist to see a list of undo branches existing in the buffer. And each branch will have a number associated with it and it is possible to move to the undo level by using the command:
:undo <number>
Anybody who has used Photoshop will find that this feature is similar to the history levels you have in Photoshop, the only difference being that in Photoshop it is for images where as in Vim it is for text.

These are not the only new features. There are scores of others like Remote file explorer which allows one to directly edit a file residing in a remote location, better POSIX compatibility, Vim's own internal grep and so on which I have not covered here because this article is after all a visual walk through of the new features in Vim 7.0.

It takes real genius and stellar coding skills to create and maintain such a versatile editor and Bram Moolenaar has proved yet again that he has the necessary ingredients to qualify him for the post.

No comments:

Post a Comment