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
:
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?
BufNewFile
to trigger the creation of an autocommand with aBufWritePost
. After the event forBufWritePost
finishes, delete theBufWritePost
autocommand.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 aBufNewFile
, check for the same cookie from aBufWritePost
, then delete it after you write the file.