Editing Files in Other Places

Thanks to seamless integration of network protocols, Vim lets you edit files on remote machines just as if they were local! If you simply specify a URL for a filename, Vim opens it in your window and writes your changes to it on the remote system (depending on your access rights). For instance, the following command edits a .vimrc file owned by user ehannah on the system mozart. The remote machine offers the SSH secure protocol on port 122 (this is a nonstandard port, providing additional security through obscurity):

$vim scp://ehannah@mozart:122//home/ehannah/.vimrc

Because we’re editing a file in ehannah’s home directory on the remote machine, we can shorten the URL by using a simple filename. It’s treated as a pathname relative to the user’s home directory on the remote system:

$vim scp://ehannah@mozart:122/.vimrc

Let’s take apart the URL so you can learn how to build URLs for your particular environment:

scp:

The first part, up to the colon, represents the transport protocol. In this example, the protocol is scp, a file copy protocol built on the Secure Shell (SSH) protocol. The following : is required.

//

This introduces host information, which for most transport protocols takes the form [user@]hostname[:port].

ehannah@

This is optional. For secure protocols such as scp, it specifies what user to log in as on the remote machine. When omitted, it defaults to your username on the local machine. When you are prompted for a password, you must enter the user’s ...

Get Learning the vi and Vim Editors, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.