sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #212: Setting file attributes without reloading a buffer

 tip karma   Rating 14/8, Viewed by 885 

created:   February 7, 2002 6:14      complexity:   intermediate
author:   Max Ischenko      as of Vim:   6.0


While creating scripts and others executable files with Vim it is needed to set UNIX executable bit on the file.
You can do this from inside Vim with :!chmod a+x %. The % represents current buffer's filename.
The problem is that Vim will notice attribute changes and prompt you to reload a file. If you do this, your undo history for the file will be lost.

The following function facilitate changing executable attributes without reloading a buffer.
Thanks to Bram for the algorithm for this function.

fun! SetExecutableBit()
let fname = expand("%:p")
:checktime
exec "au FileChangedShell " . fname . " :echo"
:silent !chmod a+x %
:checktime
exec "au! FileChangedShell " . fname
endfun

" Create an EX command that will call the function.
command -nargs=0 Xbit call SetExecutableBit()

Now you can type :Xbit to make the file executable!

 rate this tip  Life Changing Helpful Unfulfilling 

<<Rotate color themes | delet all lines containt TXT >>

Additional Notes

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo