From 3a049766e81bbb33e9d5dd03d3d95f49d7007347 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 12 Jul 2012 11:23:32 +0200 Subject: vim: add sudo plugin Signed-off-by: Florian Pritz --- .vim/plugin/sudo.vim | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 .vim/plugin/sudo.vim (limited to '.vim') diff --git a/.vim/plugin/sudo.vim b/.vim/plugin/sudo.vim new file mode 100644 index 0000000..a2c6f50 --- /dev/null +++ b/.vim/plugin/sudo.vim @@ -0,0 +1,74 @@ +" +" +" sudo.vim: A vim plugin by Rich Paul (vim@rich-paul.net) +" +" This script eases use of vim with sudo by adding the ability to +" edit one file with root privleges without running the whole +" session that way. +" +" +" Usage: put it in the plugin directory, and +" (command line): vim sudo:/etc/passwd +" (within vim): :e sudo:/etc/passwd +" +" sudo will ask for your password if need be. +" Requires: +" sudo package installed +" decent OS (sorry, no windows, unless cygwin has sudo now?) +" Provides: +" URL handler, sudo: scheme +" 2 autocommands +" +" Commands: +" SudoRead +" SudoWrite +" ToDo: +" Allow one to sudo to users other than root. +" Quote the file name better. +" + + +if exists("s:seen") && !exists("s:debug") + finish +endif +let s:seen=1 +"let s:debug=1 +function! SudoRead(url) + if a:url=="" + let file=expand(a:url) + else + let file=a:url + endif + let prot=matchstr(file,'^\(sudo\)\ze:') + if '' != prot + let file=strpart(file,strlen(prot)+1) + endif + :0,$d + call setline(1,"foo") + exec '1read !sudo cat "'.file.'" ' + :1d + set nomod + :filetype detect +endfunction +function! SudoWrite (url) abort + if a:url=="" + let file=expand(a:url) + else + let file=a:url + endif + let prot=matchstr(file,'^\(sudo\)\ze:') + if '' != prot + let file=strpart(file,strlen(prot)+1) + endif + set nomod + exec '%write !sudo tee >/dev/null "'.file.'"' +endf +command! -nargs=1 SudoRead call SudoRead() +command! -nargs=1 SudoWrite call SudoWrite() +augroup Sudo + autocmd! + au BufReadCmd sudo:*,sudo:*/* SudoRead + au FileReadCmd sudo:*,sudo:*/* SudoRead + au BufWriteCmd sudo:*,sudo:*/* SudoWrite + au FileWriteCmd sudo:*,sudo:*/* SudoWrite +augroup END -- cgit v1.2.3-24-g4f1b