lunes, 21 de octubre de 2013

vi as Python IDE

There are many editors for Python, but in this entry we are going to explain how to configure the simplest one, Vi.

Information about Used Host Operating System

Linux gon 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
Distributor ID:    Ubuntu
Description:    Ubuntu 13.04
Release:    13.04
Codename:    raring



Vi information

$ vi --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Apr  2 2013 09:17:34)
Included patches: 1-547


Basic configuration

The vi configuration file is .vimrc in home directory. An this is the aspect with basic configuration















Firstly, we are going to enable syntax coloring, automatic indentation and set line number with the command set number

" Basic configuration 
 " Automatically enable syntax coloring and automatic indentation for 
 " Python code  
 syntax on 
 filetype indent plugin on 
 " Set line numbers 
 set number 














In style guide for Python code, spaces are the preferred indentation method, then it is useful configure tag in vi equal to 4 spaces. The command for that is

  " Spaces instead of tabs  
  set expandtab  
  " 4 space for tabs  
  set tabstop=4  














Indentation in Python code is essential, so using command set autoindent always set auto indenting on.

set autoindent 

To enable file type detection we can use the following command:

filetype indent plugin on  

If you use a dark background, this command may help adjust the default colours for better contrast:

set background=dark  














You can enable code folding using the following configuration

set foldmethod=indent
set foldlevel=99

Then you will be able to be inside a method and type 'za' to open and close a fold.













Advanced configuration and tools

Lastly, we are going to configure advanced features only for brave coder.

For using this advanced configuration is compulsory the configuration of reference [1].

Tasks list
There are some coders that set mark as #TODO or #FIXME to create a list of tasks. Using the vim module tasklist the following configuration in .vimrc

 map td TaskList
 nnoremap v TaskList
 map td TaskList

you can show tasks list: hit <leader>td  ("\td")








 







Pep8

You can validate your code with  Style Guide for Python Code (Pep8) using pep8 vim plugin.

I have used the command "pip install flake8" to install  flake8 pyflakes pep8 mccabe.

Pressing F5 will run it using the "quickfix" feature.
This way you can "navigate" through errors using :cn and other standard commands.















Tab Completion and Documentation
We are going to use the SuperTab plugin to check the context of the code you are working on and choose the best for the situation.

This is the configuration in .vimrc

" SuperTab plugin
au FileType python set omnifunc=pythoncomplete#Complete
let g:SuperTabDefaultCompletionType = "context"

Pressing tab after the string for example "cl" complete with the template of a class














Besides, pydoc preview to get the most useful information out of the code completion, that gives us the ability to hit <leader>pw when our cursor is on a module














Window Splits Tool
Sometimes code folding isn't enough; you may need to start opening up multiple windows and working on multiple files at once or different locations within the same file. To do this in vim, you can use these shortcuts:

Vertical Split : Ctrl+w + v
Horizontal Split: Ctrl+w + s
Close current windows: Ctrl+w + q






















References:
[1] [http://sontek.net/blog/detail/turning-vim-into-a-modern-python-ide
[2] http://www.vim.org/scripts/script.php?script_id=2914 


No hay comentarios:

Publicar un comentario