Skip to content

Instantly share code, notes, and snippets.

@RRethy
Last active May 1, 2024 03:05
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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://jsmith@linux.student.cs.uwaterloo.ca//absolute/path/to/file.txt
@PreciousChicken
Copy link

Does the

Note: replace vim with nvim for Neovim

line hold true?

My understanding is that this does not work within Neovim (issue #8982). Or at least in my setup (Ubuntu 18.04) I can remote edit in vim exactly as above, but not nvim.

@RRethy
Copy link
Author

RRethy commented Jan 12, 2020

It works in Neovim (I use Neovim) but you have to do a bit of extra work to avoid getting the password prompt. Check the https://gist.github.com/RRethy/ad8a9a3b1112a48226ec3336fa981224#setup section on this gist which links to this article and remember to change ~/.ssh/id_dsa.pub to ~/.ssh/id_rsa.pub.

@PreciousChicken
Copy link

You are absolutely right, sorry; just tested it correct. For some reason when I read the article first it just didn't click it was mandatory to generate an authentication key for neovim. I'm relatively used to generating keys (e.g. I've done it to access university servers); but only ever to make things quicker, resultingly heuristics told me wrongly that wasn't the problem. Either way, my bad, clear enough now I re-read. Thanks for getting back to me. Great reference anyway, thanks for posting.

@sisrfeng
Copy link

"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."

Is this plugin just lie the remote-development extension in vscdoe?

I think setuping vim on the remote machine is better than using netrw.

@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