How to edit a remote file over ssh

When you need to edit a file hosted on a remote host, it will be nice if you can edit the remote file locally, as if it were a local file. Of course you could set up NFS over ssh tunnel to achieve that, but that would be an overkill if this is just one-time usage. So the question is: is there a way to edit a remotely located file over ssh?

That is when vim (short for "Vi IMproved") can help. vim can be considered "vi on steroid," with extensible features that greatly improve the vi's default editing environment. The extensibility of vim is achieved by using "plugins". One such vim plugin is netrw which enables reading and writing files over networks. netrw supports various application layer protocols including ftp, http, scp, rsync and rcp.

If you would like to edit a remote file over ssh using vim, you can do the following.

$ sudo apt-get install vim
$ vim scp://user@remote_host//home/user/path/file

Note the double "/"s for the root directory of the remote host. The above vim command opens a file located on a remote host for editing. Behind the scene, vim uses scp to fetch the requested file from a remote host, stores the file in /tmp, and finally opens it for editing.

When you attempt to save your changes to the file, the changes are first applied to a local copy in /tmp, which is then uploaded via scp to the remote host.

Since the behind-the-scene network transfer is handled by netrw plugin, you can edit any remote file using traditional vi interface transparently.


Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

4 thoughts on “How to edit a remote file over ssh

  1. Hi! First, thanks for this post and for all others. Great job!

    I wanted to point out something I think is a typo error. Should the separator between the user@remote_host and the path_to_file be a ':' instead of '/'. I mean:

    vim scp://user@remote_host:/home/user/path/file

    instead of

    vim scp://user@remote_host//home/user/path/file

    • Actually, that is not correct. In this case, you must use "/" as a separator, not ":". You probably was confused with the usual scp syntax.

      The correct usage for remote editing is:

      $ vim scp://user@remote_host//home/user/path/file

      Try it out if in doubt. :-)

  2. And this can be used in standard Emacs since 10 years ago. And you can also open archives, edit a file in it and save the file without needing to do this manually. You can even do this over the net with ssh.

    Just wanted to tell you. ;-)

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *