How do I edit a file on a remote server directly from Vim?
Answer
:e scp://user@host//path/to/file
Explanation
Vim's built-in netrw plugin supports editing files over the network using protocols like SCP, SFTP, and HTTP. You can open, edit, and save remote files as if they were local.
How it works
" Open a remote file via SCP
:e scp://user@server//etc/nginx/nginx.conf
" Open via SFTP
:e sftp://user@server/relative/path/file.txt
" Browse a remote directory
:e scp://user@server//var/www/
Note the double slash // for absolute paths vs single slash / for paths relative to the user's home directory.
Supported protocols
| Protocol | URL Pattern |
|---|---|
| SCP | scp://user@host//path |
| SFTP | sftp://user@host/path |
| FTP | ftp://user@host/path |
| HTTP (read-only) | http://host/path |
| rsync | rsync://host/path |
Saving remote files
Just use :w — netrw handles uploading the file back to the server.
:w " Save back to the same remote location
:w scp://other@host//path " Save to a different remote location
Tips
- Set up SSH keys for passwordless authentication to avoid repeated password prompts
- Use
~/.ssh/configto define host aliases for shorter URLs - Remote directory browsing works like local netrw — navigate with
<CR>, go up with- - For heavy remote editing, consider
sshfsto mount the remote filesystem locally - Netrw uses the
scpandsshcommands behind the scenes — ensure they're installed