Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use pythontidy in vim

Tags:

vim

what I am using now is ,

autocmd BufWritePost *.py !python PythonTidy.py % %

It really call the tidy programe and change the file, but the vim does not reload the new file.

And I do not want to install another plugin for it.

======================= note: I found it's dangerous about this feature, PythonTidy will will output a empty file if the command faild, it means if you have syntax error, you will lose your file unless press "u" to get it,but you can't save before you fix syntax error.

I call :!PythonTidy % % manually after pylint complete now.

like image 982
guilin 桂林 Avatar asked Oct 02 '10 05:10

guilin 桂林


1 Answers

Use BufWritePre instead of BufWritePost and combine Vim range filtering with PythonTidy’s stdin/stdout mode.

autocmd FileType python autocmd BufWritePre <buffer> let s:saveview = winsaveview() | exe '%!python PythonTidy.py' | call winrestview(s:saveview) | unlet s:saveview

(The use of autocmd FileType python autocmd BufWritePre <buffer> makes this a bit more accurate than matching on a glob pattern: it means “any time a Python file is detected, install this autocmd for that buffer” – so it works independently of file name.)

Unfortunately this cannot preserve your cursor position if you undo the filtering. (You are filtering a whole-file range; when undoing a filter operation, the cursor jumps to the first line in the range; so you end up at the top of the file.) I was hoping to find a way to create a no-op undo state, before, so you could hit u twice and get back to the right place, but I can’t make that work as yet. Maybe someone else knows how.

like image 93
Aristotle Pagaltzis Avatar answered Sep 23 '22 13:09

Aristotle Pagaltzis