在使用vim创建过程中,如何设置基于后缀的文件的权限?



当我创建一个新的python脚本时,我通常希望使其可执行。我可以通过两步完成:首先使用vim创建文件;使用chmod设置权限。问题是:是否有可能将两个步骤合并为一个?

我想看到的是:当我从vim创建文件时,它将检查后缀并设置适当的权限(可配置(。我希望它也适用于.sh、.exe等文件......谢谢。

我使用以下内容; 它检查文件的shebang(例如#!/usr/bin/python( 而不是文件扩展名。

" On the initial save, make the file executable if it has a shebang line,
" e.g. #!/usr/bin/env ...
" This uses the user's umask for determining the executable bits to be set.
function! s:GetShebang()
return matchstr(getline(1), '^#!S+')
endfunction
function! s:MakeExecutable()
if exists('b:executable') | return | endif
let l:shebang = s:GetShebang()
if empty(l:shebang) ||
   executable(expand('%:p')) ||
   ! executable('chmod')
return
endif
call system('chmod +x ' . shellescape(expand('%')))
if v:shell_error
echohl ErrorMsg
echomsg 'Cannot make file executable: ' . v:shell_error
echohl None
let b:executable = 0
else
echomsg 'Detected shebang' l:shebang . '; made file executable as' getfperm(expand('%'))
let b:executable = 1
endif
endfunction
augroup ExecutableFileDetect
autocmd!
autocmd BufWritePost * call <SID>MakeExecutable()
augroup END

最新更新