I'm aiming to make scripts I write in a certain directory (or of a certain extension) automatically executable on creation. I can do this whenever a file is written to, using the following
autocmd BufWritePost *.py silent execute "! chmod +x %"
What I don't like is that this runs every time the file is written. If I had decided to reset the permissions of a particular script, I wouldn't want it to be set again, for example. And if the command is expensive, or is not idempotent, it would be undesirable to run it again and again. While this particular example uses
chmod, please consider a more general case.
There is an event for when a buffer is created for a new file (
BufNewFile), but is there some way to do this when such a buffer is written the first time?
BufNewFileto trigger the creation of an autocommand with a
BufWritePost. After the event for
BufWritePostfinishes, delete the
help au, it doesn't seem like there's a mechanism in place to fire an autocommand on the first write only.
b:variable with an unique name) from a
BufNewFile, check for the same cookie from a
BufWritePost, then delete it after you write the file.