I'm trying to find out if it's possible to make the write ":w" command perform a forced validation for certain file types.
My use case is that I want to prevent small typos and idention errors in python source code. So VIM should not even allow me to save a file with errors.
I can run "python -m py_compile filename
" on a file to see if it compiles.
This command returns nothing, if all is well, otherwise and error message like
"Sorry: IndentationError: expected an indented block (hello.py, line 2)"
Alas this requires me to save the file "filename
, which I only want done if it passes.
So I'd need to automate these steps:
- save the current file with a temporary filename,
like
filename.insert_uuid_here.py
- run "python -m py_compile filename.insert_uuid_here.py
on success:
remove both the filename.insert_uuid_here.py and the __python_cache__/filename.insert_uuid_here.py file.
allow save
on fail:
remove the
filename.insert_uuid_here.py
display error from compile attempt
Is this something, that's possible?
How could I link this to any ":w" attempt to write a python file?
BufWriteCmd
autocommand. I would however not recommend doing that.