aboutsummaryrefslogtreecommitdiff
path: root/vim
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-07-16 01:08:20 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-07-16 01:08:20 +1200
commit260276a5461fd7d3c4cc704fc4993c0aae8414a5 (patch)
tree0028100d292f13ec760a8c41d5f4eb6ebdffa787 /vim
parentdabe814aa385f489db562d78032d404b67f4cbb9 (diff)
downloaddotfiles-260276a5461fd7d3c4cc704fc4993c0aae8414a5.tar.gz
dotfiles-260276a5461fd7d3c4cc704fc4993c0aae8414a5.zip
Add shebang_create_exec.vim plugin
Diffstat (limited to 'vim')
-rw-r--r--vim/plugin/shebang_create_exec.vim35
1 files changed, 35 insertions, 0 deletions
diff --git a/vim/plugin/shebang_create_exec.vim b/vim/plugin/shebang_create_exec.vim
new file mode 100644
index 00000000..7fabd8f7
--- /dev/null
+++ b/vim/plugin/shebang_create_exec.vim
@@ -0,0 +1,35 @@
+"
+" shebang_create_exec.vim: Make a file executable on first save if it starts with a
+" shebang.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_shebang_create_exec') || &compatible
+ finish
+endif
+if !has('autocmd') || !has('unix') || !exists('*shellescape')
+ finish
+endif
+let g:loaded_shebang_create_exec = 1
+
+" Set up hook for before writes to check the buffer for new shebangs
+augroup shebang_create_exec
+ autocmd!
+ autocmd BufWritePre * call s:Check(expand('<afile>:p'))
+augroup END
+
+" If the buffer starts with a shebang and the file being saved to doesn't
+" exist yet, set up a hook to make it executable after the write is done
+function! s:Check(filename) abort
+ if stridx(getline(1), '#!') == 0 && !filereadable(a:filename)
+ autocmd shebang_create_exec BufWritePost <buffer>
+ \ call s:Chmod(expand('<afile>:p'))
+ endif
+endfunction
+
+" Make the file executable and clear away the hook that called us
+function! s:Chmod(filename) abort
+ autocmd! shebang_create_exec BufWritePost <buffer>
+ call system('chmod +x '.shellescape(a:filename))
+endfunction