Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created October 6, 2024 20:00
Show Gist options
  • Save ryancdotorg/e6dc7484be1ea17bbcc3fe5714bd9778 to your computer and use it in GitHub Desktop.
Save ryancdotorg/e6dc7484be1ea17bbcc3fe5714bd9778 to your computer and use it in GitHub Desktop.
" automatically make script files executable when writing for the first time
function! NewScriptExec() abort
" check if this is a new file which starts with a shebang
if exists('s:new_file') && getline(1)[0:1] == '#!'
" based on https://stackoverflow.com/a/57539332
let l:file = expand('%')
let l:old_perm = getfperm(l:file)
" set the exec bit everywhere the read bit is set
let l:new_perm = substitute(l:old_perm, '\v(r.)-', '\1x', 'g')
if (l:old_perm != l:new_perm)
call setfperm(l:file, l:new_perm)
endif
unlet s:new_file
endif
endfunction
augroup new_script_exec
autocmd!
autocmd BufNewFile * let s:new_file = 1
autocmd BufWritePost * call NewScriptExec()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment