autocmd BufRead,BufEnter *.py nmap <silent><F5> <ESC> :call CheckPythonSyntax()<CR>
autocmd BufRead,BufEnter *.py imap <silent><F5> <ESC> :call CheckPythonSyntax()<CR>
function CheckPythonSyntax()
" Write the current buffer to a temporary file, check the syntax and
" if no syntax errors are found, write the file
let curfile = bufname("%")
let tmpfile = tempname()
silent execute "write! ".tmpfile
let output = system("python -c \"__import__('py_compile').compile(r'".tmpfile."')\" 2>&1")
if output != ''
" Make sure the output specifies the correct filename
let output = substitute(output, fnameescape(tmpfile), fnameescape(curfile), "g")
echo output
else
write
endif
" Delete the temporary file when done
call delete(tmpfile)
endfunction
Add a code snippet to your website: www.paste.org