jump to navigation

Convert Vi into a powerful Python IDE March 9, 2009

Posted by razasayed in Hacks, programming.

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 ! 🙂

Launch your program from within Notepad++ February 22, 2009

Posted by razasayed in programming.
Tags: , ,

Notepad++ is a good editor for programmers, that supports multiple languages, and also has some other cool features like syntax highlighting, some basic intellisense etc.. . It is free and open source , but the downside is that its available only for Windows . However, it does work on Linux too, by using Wine. Notepad++ is a good alternative for people (especially those using Windows,on Linux  vi or  emacs  are the commonly used editors) who for whatever reason are not using full fledged IDE’s , and want a simple editor with some of the bells and whistles required for their programming work.

Recently, i discovered a cool feature supported by Notepad++, which i thought id blog about . How many times have we encountered situations where we write code in an editor and then switch back to the command line to execute it , especially if we are dealing with interpreted languages like perl, python , ruby etc..

Wouldnt it be nice if we could execute the program within the editor itself  ?. Now that would come in really handy . Would definitely save us some time and overhead involved in the context switch 😉 .

Guess what, Notepad++ being plugin based , has a plugin called NppExec , which allows us to do just that .  All you have to do is download it (assuming this plugin is not already installed)  , then after extracting the archive, place the dll in <Notepad++ Installation directory>/plugins .

Heres how to use it (assuming you are using python , and have the python interpreter installed on your system , however it could be any other language for which you have the corresponding interpreter installed)

Step 1) First type in your program and save the file

Step 2) Go to Plugins->NppExec->Execute (or press F6), and in command section type


$(FULL_CURRENT_PATH) , is a Notepad++ internal variable ,that contains the full path of the file currently open in the editor . There is a list of 10 such global variables here

Step 3) Now click on Save, and type a name for this script, so that we can use it again later on.

Step 4) Press OK

Voila, you can now see the output of your program in a console window in the editor itself ! .

Then again when you make some changes to your code, you dont have to go through all the above steps again, simply press Ctrl+F6, which is the shortcut for repeating the previous command.  To switch to a different language you can press F6 and select any other language for which you have saved a script.

Tip : If you dont want those extra messages that you see along with the output, just select Plugins->NppExec->No Internal Messages

Hope someone finds this useful,

Have fun 🙂

Python programmers can fly :) January 10, 2009

Posted by razasayed in programming.
1 comment so far

Heres a nice one from xkcd :).

The Zen Of Python August 22, 2008

Posted by razasayed in programming.
add a comment

Heres a list of 19 aphorisms which serve as guiding principles for any python hacker . In the python community, a solution or piece of code conforming to these principles is said to be “pythonic”.

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren’t special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one– and preferably only one –obvious way to do it.
  • Although that way may not be obvious at first unless you’re Dutch.
  • Now is better than never.
  • Although never is often better than *right* now.
  • If the implementation is hard to explain, it’s a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea — let’s do more of those!

These principles are also distributed with python (ofcourse its “batteries-included” 😉 ), hidden like an easter-egg . Just type the following at the python interpreter prompt to view it.

import this