How to edit a remote file over SSH

Last updated on July 28, 2020 by Dan Nanni

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 Vim 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.

$ 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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean