0

I am trying to determine a way to edit remote files in Vim that require sudo, but still use my local vim config and plugins.

Currently I can open vim and :e scp://me@server//some/root/file and make changes, but when I try to write it, I of course get "permission denied".

If I try the "I forgot sudo" command (:w !sudo tee %) it tries to scp the file back as root, which is disabled. I've also tried ssh -t user@host sudo vim /some/file, but this does not source any local config.

Does anyone have a suggestion of a method I may have overlooked? Thank you in advance.

  • 1
    If that's a genuine requirement you might benefit from having your (admin) home directories on a NFS share that gets mounted on demand with the auto mounter. You'll have the same history, aliases and settings on every system you administer. In practice I have worked in too many environments and given up relying on anything customised and non-standard, I can get all sysadmin tasks done without relying on such crutches. – HBruijn Jul 11 '14 at 20:22
  • But you could sync your .vimrc with vcsh or similar tools. – zhenech Jul 12 '14 at 11:40
0

Can't you just copy your Vim configuration to the remote server, and edit it there? Assuming of course, that Vim and X is installed.

The "rsync" command does a good job of keeping everything consistent, so you can copy your Vim setup everywhere. Ideally you would then log into the remote server, and install your Vim config as root as well. :-)

Example:

rsync -avzp ~/.vimrc ~/.vim you@remote-server:~

ssh you@remote-server
sudo rsync -avzp ~you/.vimrc ~you/.vim ~root
0

You can eventually do that from vim, assuming you have sudo access on the remote server:

function! WriteRemote()
  let pat = '^scp://\(.\{-}\)/'
  let l = matchlist( bufname('%'), pat)
  if len(l) < 2
    echom "could not get remote host"
    return
  end
  let remote = l[1]
  let temp_file = expand('%:t')
  let dest_file = substitute( bufname('%'), pat, '', '')

  execute "write! scp://" . remote . "//tmp/" . temp_file
  execute "!ssh -t " . remote .
    \ " 'sudo tee >/dev/null -- " . dest_file .
    \ " </tmp/".temp_file .
    \ " ; rm -- /tmp/".temp_file . "'"
endfunction

You can just save your remote local buffer using :execute WriteRemote()

Be aware that this function has not been tested much. But you've got the idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.