Editing remote files with Vim

jovica - Mar 30 '20 - - Dev Community

One of the lesser known features of Vim is the ability to edit files remotely, over the network.

This feature comes with the netrw plugin. To achieve this, netrw uses the SSH protocol, and manages remote files via the scp command.

Here’s how to do it:

vim scp://user@myserver[:port]//path/to/file.txt
Enter fullscreen mode Exit fullscreen mode

Note the double / for the directory on the remote host, which is needed to correctly resolve the absolute path. [:port] is optional.

So with the command above you can open a file located on a remote host for editing.

What actually happens in the background is that Vim uses scp to download the requested file from a remote machine to a local /tmp directory, and then opens it for editing.

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

If you open a directory on a remote host, you could also use netrw to browse through remote files and directories. The important thing is to always specify the directory path with / at the end.

Of course, it’s recommended that you use SSH keys for authentication. Otherwise, you might be asked for the SSH password too often.

Beside SSH, there are other protocols supported such as sftp, ftp, dav, etc.

For example, to open a file on a remote FTP server, you could run a command like:

vim ftp://hostname/path/to/file
Enter fullscreen mode Exit fullscreen mode

Netrw offers lots of options and possibilities for remote editing, so for more information on this, take a look at :help scp.


What you've just read is an a section of CHAPTER 6. WORKING WITH FILES from the book Mastering Vim Quickly.

. . . . .