17 August 2013

How to write PEP8 compliant Python code on Vim

I've made this code two years ago when I wrote code for one of the popular open cloud program. This is not a standard plugin code for vim, If you have not made any configurations, you can simply follow the instructions here and download this code. This is for Ubuntu and Debian only.
I've made this seeing two links, which I don't remember right now. Sorry about that.

Please download this code (https://app.box.com/s/plc7xwm49avp4ubzq915) and follow these instructions

This is tutorial is for people who are already little known to vi editor. This is  done for vim 7.2
vim for terminal

The vimrc is so set that it would not let  write code beyond 80 lines and would automatically break. using   will
throw all the pep8 errors

Installation
-------------
Install vim7.2
Ubuntu: sudo apt-get install pep8
Ubuntu : sudo apt-get install vim
Ubuntu : sudo apt-get install vim-common (you may not need this step on all Ubuntu's)
Ubuntu : sudo apt-get install vim-runtime (you may not need this step on all Ubuntu's)
Ubuntu : sudo apt-get install vim-tiny (you may not need this step on all Ubuntu's)
copy the .vimrc from the folder to your  /home/yourusername/.vimrc
copy the folder .vim to  /home/yourusername/.vim

Checking PEP8 Complaint
-----------------------

vi somefile.py

:retab - This removes all the whitespaces mixed with tabs

Run F5

toggle between the windows using Ctrl + W twice

Toggling through a python function defintion
--------------------------------------------

Install  ctags or exuberant ctags

Ubuntu:

sudo apt-get install exuberant-ctags
rm .vim/tags/python.ctags
touch  .vim/tags/python.ctags
ctags -R -f ~/.vim/tags/python.ctags /usr/lib/your_python_library/  /home/youruser/any-python-project1/ /home/youruser/any-python-project2/ .. so on

Browsing the source code of the file
-------------------------------------
Open vi anyfile.py

in ex-mode

:helptags .

Run F3


Command in Ex-mode  - Functionality

F5 -  would run pep8 command would show list of pep8 errors (this happens only on all .py files)

CTRL + ] - would take the library function definition

CTRL + T
or - Would get back to the original file
:rew

:%s/^\s\+  - To remove  unwanted whitespaces at the beggining of lines

:retab - to remove all unwanted mix of whitespaces with tab

Note:

:w -  A simple save will automatically remove all trailing whitespaces

No comments:

Google