vim configuration for python

Gary Johnson garyjohn at spk.agilent.com
Thu May 26 16:19:27 EDT 2005


In comp.editors DJK <dkertis at gmail.com> wrote:
> Does anybody know of any scripts to check python syntax when you type
> :make?

The problem I had doing this with Python was that the Python 
interpreter spits out errors in the opposite order from that in 
which I wanted to traverse them in vim's quickfix error list.  I 
wrote the following compiler file and helper script to work around 
this.  I don't use Python very often and haven't used in it a while, 
so this still isn't very polished, but you might find it useful 
anyway.  Note also that I did this on a Unix system.

------------------------- compiler/python.vim --------------------------
" Vim compiler file
" Compiler:	Python
" Maintainer:	Gary Johnson <garyjohn at spk.agilent.com>
" Last Change:	2002-06-26 00:27:56

if exists("current_compiler")
    finish
endif
let current_compiler = "python"

setlocal makeprg=python\ %
setlocal shellpipe=2>&1\ \|\ ~/.vim/tools/efm_filter.py\ \|\ tee
" Note:  efm_filter.py could be rewritten to send its input to stdout
" and to write its output to the file given on the command line.  This
" way the user could see the familiar output from Python while the
" quickfix error file received the format the vim can understand.  E.g.,
"
"     setlocal shellpipe=2>&1\ \|\ ~/.vim/tools/efm_filter.py
"
" or
"
"     setlocal shellpipe=2>&1\ \|\ tee\ /dev/tty\ \|\ ~/.vim/tools/efm_filter.py

setlocal errorformat=\ \ File\ \"%f\"\\,\ line\ %l\\,\ in\ %*[^\\,]\\,\ %m
------------------------------------------------------------------------

------------------------- tools/efm_filter.py --------------------------
#!/usr/bin/env python

import sys
import string

errors = sys.stdin.readlines()

message = errors.pop()[:-1]
errors.reverse()

for error in errors:
    if string.find(error, '  File') == 0:
	print error[:-1] + ", " + message
	message = "Traceback"
------------------------------------------------------------------------

To use these, put them in the indicated directories under ~/.vim and 
execute ":compiler python".  Also see ":help compiler".

HTH,
Gary



More information about the Python-list mailing list