" " " 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. " " " if exists("s:seen") && !exists("s:debug") finish endif echo 'hi' 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