Skip to content

Instantly share code, notes, and snippets.

@RRethy
Last active November 21, 2024 06:26
Show Gist options
  • Save RRethy/ad8a9a3b1112a48226ec3336fa981224 to your computer and use it in GitHub Desktop.
Save RRethy/ad8a9a3b1112a48226ec3336fa981224 to your computer and use it in GitHub Desktop.
Seamlessly editing remote files in (Neo)Vim with Netrw and scp

Seamlessly editing remote files in (Neo)Vim with Netrw and scp

Neovim and Vim both come bundled with a standard plugin called Netrw. Netrw acts a file explorer (similar to NERDTree), but more importantly has the ability to work with scp (as well as sftp, rcp, ftp, and lots of others :h netrw-nread) to let you edit files and browse directories that are hosted on a remote machine, inside of your local Vim instance.

This is useful since you are able to use your Vim setup and plugins without copying over your dotfiles to the remote machine. As well, since the file is copied to your local machine, there will be no delay when typing.

Setup

This is optional for Vim, but required for Neovim (check this Neovim issue explaining why).

Typically, each time you use scp, you must input your password. This can be tedious especially since this means each time you read or write a remote file/dir, Netrw will prompt you for your password.

You can avoid having to input your password for each scp and ssh session by following these instructions or by following these instructinos for OSX (~/.ssh/id_dsa.pub should be ~/.ssh/id_rsa.pub).

Editing Remote Files

You can start Vim to edit a remote file with the following:

$ vim scp://[user@]machine//absolute/path/to/file.txt
or
$ vim scp://[user@]machine/relative/path/to/file.txt

Note: For an absolute path, you must have the double backslashes before the path.

Note: replace vim with nvim for Neovim

If you are inside Vim then you can read a remote file as follows:

:Nread scp://[user@]machine//absolute/path/to/file.txt
or
:Nread scp://[user@]machine/relative/path/to/file.txt

If you open a remote file with :Nread then you can save your changes with :w and the remote file will be written to.

If you want to create a file on your remote machine. You create it as you would normally, then write it using :Nwrite as follows:

$ vim myfile.txt

:Nwrite scp://[user@]machine/relative/path/to/file.txt

Browsing Remote Directories

Since Netrw doubles as file browser, you are also able to browse a remote directory in Vim. Use the same command above, but terminate the path to the directory with a / (this is a must).

:Nread scp://[user@]machine//absolute/path/to/dir/
$ vim scp://[user@]machine//absolute/path/to/dir/

Additional Notes

For more info

Open Vim and type the following:

:h netrw

Latency

Since you are copying the remote file, there will be a latency with most commands (But no latency when typing since you have a local buffer of the file). Browsing a directory, writing a file, read a file will all have a noticible amount of latency. All latency will be blocking so you will not be able to use Vim while the scp is being done.

Examples

scp was used as follows:

$ vim scp://[user@]machine//absolute/path/to/file.txt

If your username was jsmith and the remote machine was linux.student.cs.uwaterloo.ca then the complete command would be:

$ vim scp://[email protected]//absolute/path/to/file.txt
@RRethy
Copy link
Author

RRethy commented Mar 25, 2022

Feel free to setup Vim on a decade old remote machine, I'm sure Vim 7 will satisfactory and you won't have any input latency. netrw is a lot older than vscode and solves a different problem than it's remote functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment