VIM, an editor with batteries included

../images/vim/fotillo.jpg
Author:Hugo Ruscitti
Bio:Informatics Student in the Universidad Tecnológica Nacional (UTN: National Technology Univ.). He has worked creating linux distributions and is currently an associate in gcoop work cooperative. His greatest hobby is videogame programming, activity he promotes and carries out together with his site's members at http://www.losersjuegos.com.ar/
Email:hugoruscitti@gmail.com
Web:www.pilas-engine.com.ar
../images/vim/logo.png

To write python code you could use any text editor, there are a lot of great options and different approaches to development

But from all the editors I know, there is one that deserves special attention, not only because of its huge user-base but also because of its features and quirks. Yes, I'm talking about VIM.

Why VIM?

If you use a computer a lot, and spend most of that time programming, you will surely care about the editor you use. The more experience you have with writing code, the less you want to repeat yourself and want to focus on making important changes as fast as possible.

VIM is a text editor developed for those cases, an editor written by programmers (obviously) and for programmers. One of the keys to its success are its commands and plugins to make day-to-day tasks quicker with as little key-stroking and as directly as possible.

For example, in Python's case, it makes browsing the structure of very big projects really simple, has code auto-completion and eases making changes of portions of the code.

A simple example

Picture this: you downloaded a python library from a repository and are investigating how it works.

To open VIM and explore the project's files you could write something like the following in your console:

vim projects_folder

And the editor will show a list of files to inspect. If you press ENTER on any file it would just open it. And it does syntax highlighting.

If you now want to go to a specific portion of the code, it is very likely that you have an idea of the text you expect to fine. For those cases, VIM has quick-searching that you activate pressing "/".

For example, if you want to browse through the functions of a file you write "/def" (the "/" starts searching and "def" is what identifies python's functions).

../images/vim/def.png

Now, pressing ENTER will stop searching. If you press "n" it will search for the next match, and "N" the previous one.

Lets imagine that there is a method name you are interested in. If you press "W", VIM will jump to the next word (the name of the method or function) and then pressing "*" it will jump to the next appearance of that function.

This last shortcut is very usefule to highlight reference names in a portion of the code.

Auto-completion

Auto-completion is quite complex, you can auto-complete filenames using CTRL+x CTRL+F, complete words by repetition with CTRL+p or even use context-based auto-completion, for example within a python program pressing CTRL+x CTRL+o:

../images/vim/omni.png

The only bad thing about these auto-completion types, is that they are a tad complicated to invoke. A way to overcome that issue is to install a plugin called SuperTab that invokes auto-completion when you press the TAB key (and is intelligent enough to allow us to keep using TAB for indenting).

Quickly locating files

Something quite common in software projects is that you need to access and switch between several files.

VIM has several options for that: you can use tabs with commands like :tabnew file_name or split your window with commands like :split or :vsplit.

But if you want something immediately, a plugin like FuzzyFinder comes handy as it allows you to find files quickly while you write its name (no matter the folder):

../images/vim/fuzzy.png

There is even another plugin called MRU that opens a window with recently-modified files.

Quick navigation

VIM allows for navigation within a big project jumping from a function's invocations and its declaration.

To use that feature, we first need an external tool called ctags that prepares the project to be explored:

cd projects_folder · ctags -R *

This enables a lot of features; for example, we could open the project with the vim . command and then write :tag *cli and press TAB. VIM will locate all classes, functions or references that have cli in them. En my case, it is offering two symbols; if I pressed ENTER it would jump directly to their declarations:

../images/vim/tag_cli.png

Another interesting functionality that is enabled by the use of ctags is that you can place your cursor over the name of any class, function or reference and pressing CTRL+] will make VIM jump directly to where it is defined. Just like a link!

Possibilities just cascade from here, because now the editor knows exactly where each thing in your program is defined and can take advantage of it.

You can even install something like TagList to see a panel with your class' interfaces:

../images/vim/tags.png

The right portion of the previous image shows a tree that provides direct access to several classes. That is very useful when you are working with several big classes and you need to jump all around following your application's flow.

Configuring your environment

It is important to mention at this point that one of the main points of VIM is its configuration.

Unfortunately, the editor has a very bad default configuration so it is normally a very good idea spend a few minutes researching about configuration parameters in blogs that belong to python programmers that use VIM.

For example, the following option in your ~/.vimrc file means that pressing <F2> will make the script you are editing at the moment to be run, something very useful when prototyping:

map <F2> :w!<cr>:!python %<cr>

Here are some links where people suggest how to customize the editor:

Conclusion: be patient

VIM has a lot of interesting features and it would take a life to see all of them. It takes some time since you start using it until you feel comfortable with it, but is becomes more and more valuable and fun with time. It makes you think about quicker and simpler ways to do stuff.

So, if you are searching for a good text editor, my recommendation is that you take VIM into account and start using it with some patience...

Help PET: Donate

blog comments powered by Disqus

Last Change: Thu Sep 22 08:54:01 2011.  -  This magazine is under a Creative Commons license