vim: create file with +x bit

I don't recall where I found this, but I use the following in my ~/.vimrc

" Set scripts to be executable from the shell
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif

The command automatically sets the executable bit if the first line starts with "#!" or contains "/bin/".

I found this script at Not a perfect solution, but a acceptable one, I think.

function! SetExecutableBit()
  let fname = expand("%:p")
  execute "au FileChangedShell " . fname . " :echo"
  silent !chmod a+x %
  execute "au! FileChangedShell " . fname
command! Xbit call SetExecutableBit()

You can now set the execute bit with the command :Xbit. All credit to Max Ischenko at