I've found this question that explains how to edit a remote file with vim using:

vim scp://user@myserver[:port]//path/to/file.txt

Is it possible to do this as root (via sudo) on the remote host?

I've tried creating a file with root permissions on the remote host and editing it with the above. Vim can see the content, can edit it, and can save it but nothing changes on the remote host (probably because vim is just saving its temp file and then giving that to scp to put back?)

When doing this with a file saved by my user it behaves as expected.

My SSH uses a key to authenticate and the remote server has NOPASSWD for my sudo access

This question is similar, but the only answer with votes uses puppet which is definitely not what I want to use.

Edit: In response to @drewbenn's comment below, here is my full process for editing:

vim scp://nagios//tmp/notouch

Where /tmp/notouch is the file owned by root, I see vim quickly show

:!scp -q 'nagios:/tmp/notouch' '/tmp/vaHhwTl/0'

This goes away automatically to yield an empty black screen with the text

"/tmp/vaHhwTl/0" 1L, 12C
Press ENTER or type command to continue

Pressing enter allows me to edit the file

Saving pops up the same kind of scp command as the beginning, which quickly and automatically goes away (it's difficult to read it in time but the scp and /tmp/... files are definitely there)

  • 2
    Just ssh to the server and edit the file there (with visudo, or whatever). Anything else has security implications. Jun 27, 2016 at 16:44
  • @SatoKatsura I fail to see any real security implications other than having a copy of the remote file on your localhost. I want to use my local version of vim due to the version and for my .vimrc file
    – Mitch
    Jun 27, 2016 at 16:47
  • Why not instead use configuration management to push (or pull) the files out to production? Then you can just use your local editor.
    – thrig
    Jun 27, 2016 at 16:51
  • @drewbenn I've updated the question with an edit showing the full process I'm using
    – Mitch
    Jun 27, 2016 at 16:52
  • Hmmm... doesn't this work? ssh -t host sudo vim file?
    – Kusalananda
    Jun 27, 2016 at 17:02

4 Answers 4


I'm going to say this is not possible because vim is not executing remote commands. It is simply using scp to copy the file over, edit it locally and scp it back when done. As stated in this question sudo via scp is not possible and it is recommended that you either modify permissions to accomplish what you're wanting or just ssh across to the remote machine.

  • Not the answer I was hoping for, but seems to be the correct one. Maybe I'll write a plugin to get around this..
    – Mitch
    Jun 27, 2016 at 19:16
  • Hey @Mitch, did you find any solution/plugin to address your problem? I'm experiencing exactly the same situation! Thanks! Aug 31, 2017 at 11:59

Like the accepted answer, I don't think this is possible directly.

However, I see at least two ways to still accomplish your goal.

Running vim remotely

ssh user@myserver sudo vim /some/file

This has disadvantages:

  • Your interactions with vim go over the network. Significant lag will be annoying, and if your connection dies, so does vim (eventually).
  • This won't use your local vim config, but remote's root's vim config.

But it has the advantage of working.

Doing the scp outside of vim

You could just copy the file over locally, edit it, and copy it back. And you could automate that to make it almost as seamless as vim's scp support.

Something like the following shell script could work (note, fully untested!):

#! /bin/sh

ssh -- "$1" sudo cat "'$2'" > ${TMPFILE}
vim ${TMPFILE}
ssh -- "$1" "sudo tee '$2' > /dev/null" < ${TMPFILE} && \
  rm -f ${TMPFILE}

This would allow you to do something like rvim user@myserver /some/file. It even keeps the local copy if the second transfer fails, so you don't lose your changes.

The script could use many improvements (at the very least error checking), but it's a starting point.


You would need the root password or have your public ssh key in ~root/.ssh/authorized_keys. Once you had that, you could probably do

vim scp://root@nagios//tmp/notouch

Bottom line: this is effectively just a shortcut for

scp root@nagios:/tmp/notouch /tmp/notouch
vim /tmp/notouch
scp /tmp/notouch root@nagios:/tmp/notouch

If you have the necessary access to do that, then you have the necessary access to use vim's network access plugin. If not, then you don't.

As Zachary Brady points out, sudo is not involved. You'll need ssh access to the root account.

Have you tried it?

  • 1
    I don't have the root password, and I'm not sure I feel comfortable putting my key in there without permission from the people who own the server. The true shortcut that it's doing for me is saving me from ssh-ing, sudo cp-ing, scp-ing, editing, scp-ing, ssh-ing, sudo cp-ing (as my user cannot read the file). sshfs may be the way to go here
    – Mitch
    Jun 27, 2016 at 19:02
  • There's a good chance that sshfs won't work either if you don't have ssh access to root. The remote file system would likely be mounted with only your permissions. I think at this point, your only option is ssh remotesystem sudo vim file Jun 27, 2016 at 19:07
  • You're right - sshfs doesn't work (and you pointed out to me that I had accidentally chowned a bunch of files to myself that shouldn't have been...) though I thought it was working
    – Mitch
    Jun 27, 2016 at 19:14
  • 1
    @Mitch - The people who own the server gave you an user account, and, i believe, you're accountable for what you use that account for. You're placing there a public key which anyone with access to that account could. You're tying the permissions of that account to the possession of the private key just as much as the knowledge of the password allows. i.e. form the server's point of view there is no difference between having a key or knowing the password.
    – grochmal
    Jun 27, 2016 at 20:20
  • Bottom line: I think you're right to hesitate to put your public key on that server. I think you're stuck with ssh/sudo vim Jun 27, 2016 at 20:45

Prerequisite for this workaround (using rsync instead of scp):
you must have passwordless sudo on the remote machine1

1.) first change the rsync command in your local vim setup:

:let g:netrw_rsync_cmd='rsync --rsync-path="sudo rsync"'

so on the remotehost "sudo rsync" is executed, not just "rsync", see :h netrw-externapp

2.) read the file:
After the hostname you have to write :/filename not //filename (as usual in vim?!)

:e rsync://myhost.net:/etc/apt/sources.list

3.) write the file

:e rsync://myhost.net:/etc/apt/sources.list

x.) to print possible error use:

:verbose e rsync://myhost.net:/etc/my_file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .