Monday, February 15, 2010

The Vim mode

Vim Forever...

It all started when I saw Venkatesh Choppella working without taking his fingers off the keyboard to do many things which usually required shifting my hand between keyboard and my tailed friend, mouse. I knew for sure that he was using emacs. It was cool and I was fascinated about this cute little tool. My friend Puneeth uses it all the time for editing. He also has some crazy extension on firefox that troubles me while using. It is emacs extension.

Anyways now I wanted to use something cool like that. I know for sure that Vi is the competitor and so I started by googling for editor wars. I found many links that support emacs. So I started learning emacs rather looking at emacs. First thing I didnt like was the use of control key so often which required shifting of my fingers way down the home row. Then I had problem shifting my fingers to use the arrow keys for navigating through the file.

So here I am left with no other option than vi. I have used vi for quite a while and am pretty comfortable with it. But all I do in vi is first get into insert mode and edit the file as if I am using notepad. I started searching for tutorials on vi and found one that is so simple yet so amazing. I thank Swaroop CH, yes the same guy who wrote the book "A Byte of Python", for another amazing book "A Byte of Vim". It was so amazing and I started using vi like a novice vimmer.

Now editing a text file is something I do only once in a while. Most of the time I either code in Python or browse on Firefox. Ofcourse watching stuff and listening to music is also there but doesn't make sense in this context. So now what is this emacs plugin for firefox and is there something similar for Vim???. Yes there is one and it is called Vimperator. It transforms firefox into something that behaves similar to Vim editor and hence very seldom do I need the mouse. It is a totally different experience using firefox in Vim mode.

Now comes the most important job. Making Vim an ide for Python.
Let me start by listing a few things one does on a typical ide.
  1. A file browser that stays on left of screen and enables opening files on one click
  2. Ability to show multiple files in one screen
  3. Syntax highlighting and code folding
  4. Taglist that displays all the functions, classes and stuff in one place
  5. Auto Indentation
  6. Code completion
  7. Debugging
Phew!! finally the list ended.
Lets start with the first, The File browser
Vim has a very simple and light file browser plugin called NERDTree.
Installing is as simple as extracting the files into ~/.vim directory.
Using :NERDTree brings up the nerdtree and a tree structure is displayed.
Moving between files is the same as it is in normal mode. The usual hjkl work and <CR> or the enter key is used to expand or collapse a directory. Clicking enter on a file opens the file in edit mode. 'o' key can also be used to do the same. <ctrl+w> <ctrl+w> is used to switch context between nerdtree and the file buffer. 'i' opens the file in a new screen.

Showing multiple files.
This is as simple as splitting the screen into parts and showing different files on each screen.
:sp splits the screen horizontally and :vsp splits vertically.
If you notice, we have seen something similar while using nerdtree. What it is doing is simply opening another buffer for itself.
<ctrl+w> <ctrl+w> can be used to switch context. The usual vim way of prepending a number also works here. so 4 <ctrl+w> would execute <ctrl+w> command four times. can also be mixed with hjkl.

syntax highlighting and code folding
vim takes care of syntax and all we have to do is :syntax on. You can also download a syntax file from here or write one of your own and name it python.vim. Installing is as simple as placing it in ~/.vim/syntax folder.
code folding is a little different for different contexts. For python, the obvious is indentation. First do :foldmethod=indent. Then scroll to your block of code and za toggles the folding of code.

taglist is a plugin for vim that supports many languages. It requires a package called "exuberate ctags" which should be directly available on repo for any distro. Then download the zip file and extract contents to ~/.vim and you have your plugin.
The visibility of taglist can be toggled using :TlistToggle and it works similar to nerdtree.

Autoindentation
following pep8, here are a few settings that should make your life easy.
  • set expandtab
  • set textwidth=79
  • set tabstop=8
  • set softtabstop=4
  • set shiftwidth=4
  • set autoindent
at this point you will notice that you have to type the 4 spaces or tab for the first time and then the code is auto indented from the next line.
Wouldn't it be great if we didn't have to bother about even touching the tab key and the editor does that depending on the context. All that is just one step away. Download this script and place it in ~/.vim/indent folder. Now use the command ":filetype plugin indent on" or add the line "filetype plugin indent on" to your .vimrc file in home folder and there you go. Vim does all your indenting.

If you would have noticed, we have to type in all these conf settings every time we start vim. We don't want vi to do these settings for all the files either. What if we could set things only for files that end in .py. There is a very simple way of accomplishing that. Change the "set" to "setlocal" and put all your settings in a python.vim file and place it in ~/.vim/ftplugin.
So your python.vim would look like this:

setlocal expandtab
setlocal textwidth=79
setlocal tabstop=8
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal autoindent

Code completion and Debugging are for a different set of audience ( not me ).
But shall blog about it soon.

4 comments:

  1. My name is Swaroop C H, there is no Kumar in my name :)

    Welcome to the Vimmers gang :)

    ReplyDelete
  2. @swaroop
    Oh... corrected :).
    Thanks a million for that amazing book.

    ReplyDelete
  3. /me ignores this post. :P

    ReplyDelete
  4. I use emacs just like punchagan. Simple reason: I feel vim is great when editing alot, and not writing much, and Emacs is other way around. Since I too write mostly in text, I use emacs, coz there is very little I have to edit.

    ReplyDelete