Armin Ronacher

Vim as Development Environment

written by Armin Ronacher, on Monday, April 2, 2007 12:23.

I do all of my development (well, except of some Java stuff for school) using vim. This small article should show some of the plugins I use because quite a few people asked me for them.

Introduction

First of all I have to defend vim. Many people think vim is a hacker tool and real developers (apparently hackers are not developers?) use IDEs. Well, that's only partially true. I use a IDE for C# and Java too but that has to do with the fact that those languages are completely different to ruby, python, HTML markup or whatever I use vim for. And if there would be an vim Ecplipse plugin I'm sure that I would use it.

Vim is still the best editor for me. The graphical version has support for many colors (i like them ^^), many different syntax schemes and it handles nested syntax blocks hell (eg, jinja templates in HTML).

Theme

For the plugins listened below I wrote a theme for myself which adds more colors to the syntax elements provided by them. The theme has different colors for most of the syntax elements and is optimized for python, HTML, django/Jinja, JavaScript and Ruby.

It's quite colorful (too colorful for many ^^) and is a GUI only theme. See the screenshots below for some examples.

Syntax Files

The default vim syntax files are nice but not colorful enough for me. Especially the default HTML and Python files are crap in comparison what you can get on vim.org. However I did some local changes on some of the files for my needs and I don't upgrade those scripts because of that. If you want exactly the files I use have a look at them.

Note that some of them mainly exist because they are a requirement for another file (all those svn files belong to each other, mathml and svg belong to html). The eruby.vim file is my own one with support for % lines not supported in the version from the vim webpage and it also handles comments correctly.

Plugins

The same applies for the plugins. I use the minibufexplorer (interestingly I don't use vim 7 tab feature myself), the close tag plugin and the textmate snippets emulation called SnippetsEmu. Both plugin unchanged from the vimscripts webpage:

Snippets for SnippetsEmu

I don't use the original snippets because when I first tried out SnippetsEmu it shipped some i don't wanted to use and those I would have used were completely broken. Because I haven't upgraded the plugin I still use mine. If are interested in, here they are:

vimrc

Okay. Now to my vimrc. In order to get that thing working you have to install all of the plugins from that page. At least I guess so because it configures a lot of the plugins here.

Tips And Tricks

Here some various tips and tricks for vim I use every day.

The * and # Keys

In normal mode you can use * and # to search for the word under the cursor. * search forwards, # backwards.

The . Key

Also useful is the . key. In normal mode it repeats the last change. Very useful for XML editing and where you have to do similar stuff.

The % Key

Pressing the % key in normal mode while being on a parenthesis or a similar construct jumps to the opposite parenthesis. There are a couple of plugins that extend that functionallity but the default behavior is usually good enough for most of the programming languages.

The < and > Keys

When working with Python you often have to indent or outdent a couple of lines. Just mark them using the visual mode and press > to indent them. press 4< to outdent them 4 steps etc.

Advanced Undo Features

Vim has a different system of undoing changes. The normal undo is u in normal mode and ^r is redo. But what happens if you undo a few things and then change something? A normal editor forgets about the new changes. It's not possible to redo that again.

In Vim it's different. Vim starts a new undo branch. Using :undolist you can have a look at the possible undo states. With :earlier 20s / 1m / 2h you can then go back 20 seconds, one minute etc. Traveling forward in time works using :later and a timedelta as argument.

The Search Feature

I really like the firefox search feature. Just hit "/" and you can search in the current file and get the results in real time. That works with vim exactly the same. All you have to to is to either add :set incsearch into your vimrc or type it into the command prompt. Using n you can go to the next result. If you want to have all the search results highlighted use :set hlsearch. Hiding the results works using :nohl

Closing XML Tags

If you have the closetag.vim plugin installed (link above) you can add this to your vimrc in order to get the feature working: autocmd FileType html,xhtml,xml source ~/.vim/scripts/closetag.vim (update the path to your installation and your filetypes of course) Once this is done you can close open tags using ^_.

Regexp Replacement after Search

If you searched for a text using /foo and you now want to replace the found results with something you don't have to write this regexp again. Just do :%s/<c-r>//replacement/g and <c-r> is automatically replaced with the last search regular expression.

Using Bookmarks

Bookmarking in vim is darn easy. If you are on the current line just bookmark it with mX where X is a lowercase letter from a to z. Go to that mark using 'X where X is the same letter again. Using '' you can jump back to the position you were before jumping to the bookmark. You can get the list of bookmarks using :marks.

The Vim File Browser

The Vim File browser is a nice thing. If you open a file using :e and that file is a directory you get a nice file browser for the files in that directory. If you are a python developer you probably want to filter some files out (*.pyc etc). Add this to your vimrc: let g:explHideFiles='^\.,.*\.pyc$' This hides pyc files as well as hidden files.

The Wildmenu

The best vim feature is the wildmenu. Add a set wildmenu to your vimrc and discover the possibilities of filesystem surfing ^^ Enter :e in the command line and press ^D. Vim will show you all possibitilites in a nice little window. By entering the start of a filename and pressing tab it completes for you then. If it was a folder you can now press ^D again to get the contents. Once you finished the command this window will disappear again and you can continue working. Works of course for all commands not only the open command.

Vim Completion Features

Last tip but maybe the most powerful one :). If you have vim7 and omni completion for your language you can use ^X^O to get the completion similar to intelli sense or how it's called. Note that the default color (pink) can be overridden in the theme. In my fruity.vim file i use this to get white letters on a dark red background: hi Pmenu guifg=#ffffff guibg=#cb2f27

Also nice is ^N. It looks up all used words in the open buffers and presents then in a dropdown completion. Useful if you have long variable names and don't remember them. Just type in the start of the variable name and press ^N. Vim will either complete it or show the list of possibilities.

Comments

  1. The only thing vim lacks is a 'cure cancer' plugin. Great post.

    —  el_rob on Saturday, April 11, 2009 19:23 #

  2. —  Sergio Luiz Araújo Silva on Monday, May 11, 2009 19:51 #

  3. Ouch! Hot post :)
    The power of vim amazes me more and more.
    At the beginning I was looking for something crossplatform to replace TextMate. Decided to give gvim plus snipMate.vim a try and now I see it is much more.

    —  Nek on Sunday, June 21, 2009 23:03 #

  4. thanks for sharing!

    it's obvious that you have a lot of vim experience

    cool

    —  denstu on Tuesday, June 30, 2009 19:28 #

Leave a Reply