tags:

views:

80

answers:

2

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.

+1  A: 

Based on :help :e:

                                                        *:e* *:edit*
:e[dit] [++opt] [+cmd]  Edit the current file.  This is useful to re-edit the
                        current file, when it has been changed outside of Vim.
                        This fails when changes have been made to the current
                        buffer and 'autowriteall' isn't set or the file can't
                        be written.
                        Also see |++opt| and |+cmd|.
                        {Vi: no ++opt}

So you'd need to use :e after updating the file externally. However, :! doesn't let you use | normally (see :help :!), so you need to wrap it:

autocmd BufWritePost *.py execute "!python PythonTidy.py % %" | e

(:autocmd doesn't interpret | normally either, which is why you don't need to escape it yet again.)

Roger Pate
got an error, /bin/bash: e: command not found
guilin 桂林
! is call outside
guilin 桂林
@guilin: How about this edit?
Roger Pate
autocmd BufWritePost *.py !python PythonTidy.py % % autocmd BufWritePost *.py e , I seprate it to 2 lines, It reload the file, but it lost file syntax.
guilin 桂林
@guilin: "syntax on" after "e" will reload that, but I'm starting to think the initial approach is wrong. Sorry I don't have more helpful advice.
Roger Pate
any way it works great. thanks a lot. autocmd BufWritePost *.py e | syntax on
guilin 桂林
Reloading the file will lose all of your undo tree. This is not a good solution.
Aristotle Pagaltzis
@Aristotle: Doesn't that depend on the setting of undoreload?
Roger Pate
`E149: Sorry, no help for undoreload`
Aristotle Pagaltzis
@Aristotle: It's here in 7.3; perhaps it's new.
Roger Pate
I’m using 7.2, so it’s brand new. Is it possible to undo the reload, like it is possible to undo a filter operation? If not, that would be another argument against this solution. A third one is that it requires a named file, which can be inconvenient in some situations.
Aristotle Pagaltzis
@Aristotle: Yes, I know there are problems with this answer; I like yours much better.
Roger Pate
+2  A: 

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.

Aristotle Pagaltzis
au filetype python au ... is really good snip
guilin 桂林
Note that you can glob-match file types too, eg. you can write `autocmd FileType {xml,xslt,xhtml}` to make an auto-command apply to any files detected to contain either XML, XSLT, or XHTML. This is especially useful for file types that can be detected by content rather than file name.
Aristotle Pagaltzis
You don't even need the braces, to use another application of comma there. (:help file-pattern)
Roger Pate
Ah, that’s even cooler.
Aristotle Pagaltzis