jump to navigation

Convert Vi into a powerful Python IDE March 9, 2009

Posted by razasayed in Hacks, programming.
Tags:
9 comments

Below is a simple two step recipe for converting Vi into a full fledged IDE for python . Though there are numerous powerful features which can be added to Vi, to make it more powerful as an IDE, for simplicity’s sake and also to enable newcomers to easily get started with using some of these great features, im gonna share a couple of ways in which you can use Vi as a Python IDE.

These tips would enable you to get all those cool features like code completion , syntax highlighting , automatic indentation in Vi which would definitely make you more productive as a python hacker.

Step 1) For automatic code completion, download the pydiction plugin for Vi, from http://www.vim.org/scripts/script.php?script_id=850

Then, you need to extract this file into  your .vim directory. Assuming its in your home directory e.g.) /home/raza/.vim (If .vim directory does not exist create it), you run the following command from the location where you dowloaded the plugin

tar xvzf pydiction-0.5.tar.gz -C ~/.vim

To use auto-completion, simply press Ctrl-n or Ctrl-p , while in Insert mode.

Step 2) Now, add the following piece of code to your .vimrc file. This file is located either in your home directory or in /etc/vim.
If you dont want to risk messing up your existing .vimrc, you can create a backup of it, and create a new .vimrc file and then paste the following code in it:

[Note:] Dont forget to replace /home/raza/.vim in the code below with the location of your .vim directory

set nocompatible
syntax on
set showmatch
set ignorecase
set showmode
set ts=4
set sw=4
set autoindent
set smartindent
set number
autocmd FileType python runtime! autoload/pythoncomplete.vim
imap <c-space> <c-x><c-o>
:set backspace=2
if has("autocmd")
        autocmd FileType python set complete+=k/home/raza/.vim/pydiction-0.5/pydiction isk+=.,(
        endif " has("autocmd"

filetype plugin on
filetype indent on

" Mappings
nmap <C-N> :noh <CR>

Thats all there is to it ! . If you want to directly execute your program , without having to leave vi, then switch to command mode and execute the following command:

:!python <yourfilename>.py

There are more cool features like adding support for snippets, debugging etc that can be added to Vi, but maybe ill cover them in a later post.
If readers would like to share any more tips for customizing Vi for python development, then you are welcome to post them in your comments.
Hope this makes your python programming more productive and fun.

Happy Hacking ! 🙂

Advertisements