Vim - Remote File Editing
Vim - Remote File Editing
Section titled “Vim - Remote File Editing”Working with files on remote servers is a common task. While SSHing into the server and editing files there is an option, Vim allows you to edit remote files directly from your local Vim instance, leveraging your local configuration and plugins. This is primarily facilitated by the built-in netrw plugin.
- Accessing remote files
- Understanding how
netrwhandles reading and writing - Other supported protocols
Accessing Remote Files with netrw
Section titled “Accessing Remote Files with netrw”Vim’s netrw plugin (enabled by default) allows you to edit files over a network. The syntax for opening a remote file is:
vim <protocol>://[user@]hostname/<path_to_file>For example, to edit /tmp/message.txt on remote-server.com as user john using SCP:
$ vim scp://john@remote-server.com//tmp/message.txtNote the double slash // after the hostname for an absolute path on the server. A single slash / would be relative to the user’s home directory (e.g., scp://john@remote-server.com/project/file.txt). Vim will prompt for a password or use SSH keys if configured.
You can also open remote files from within an already running Vim session using the :edit (or :e) command:
:e scp://john@remote-server.com//etc/config.confOnce the file is open, you can edit it like any local file. When you save with :w, netrw handles transferring the file back to the remote server.
Understanding netrw Reading and Writing
Section titled “Understanding netrw Reading and Writing”When you open a remote file, netrw fetches it to a temporary local file. You edit this local copy. When you write the file (e.g., with :w), netrw transfers the contents of this temporary file back to the remote location.
The original tutorial mentioned :Nread and :Nwrite. These are lower-level netrw commands. Typically, users interact with netrw transparently through standard Vim commands like :e, :w, :split, etc., applied to remote paths.
For example, to explicitly read a remote file into the current buffer (less common than :e):
:Nread scp://john@remote-server.com//tmp/status.logAnd to write the current buffer’s content to a remote file:
:Nwrite scp://john@remote-server.com//var/www/html/index.htmlHowever, using :e remote_path and then :w is the more conventional workflow.
You can also browse remote directories by opening a directory path:
:e scp://john@remote-server.com/projects/This will open a netrw browser window listing the contents of the remote directory. You can navigate and open files from there. For more details, see :help netrw.
Other Supported Protocols
Section titled “Other Supported Protocols”Besides SCP, netrw supports several other protocols for remote file access:
- FTP:
ftp://[user[:password]@]hostname/path - SFTP:
sftp://[user@]hostname/path(often preferred over SCP for interactive use) - HTTP/HTTPS:
http://hostname/pathorhttps://hostname/path(usually read-only) - rsync:
rsync://[user@]hostname[:port]/path(for transferring files, often requiresrsyncinstalled locally and remotely) - DAV (WebDAV):
dav://[user@]hostname/pathordavs://...for secure WebDAV
The availability and behavior of these protocols can depend on your Vim compilation and system configuration. SFTP is a robust option if your server supports SSH.
Alternative Approach: For more seamless remote editing, some users prefer mounting remote filesystems using tools like sshfs at the OS level. Once mounted, the remote files appear as local files to Vim and all other applications.