Author: Eiko

Tags: vim, remote edit, netrw, remote files

Time: 2024-09-18 21:27:44 - 2024-09-18 21:29:29 (UTC)

What are the ways to edit remote files with vim?

There are several different ways vim can be used to edit remote files,

Copy back and forth

  • pros:
    • zero latency
    • no need to install anything on remote machine
    • you can use your own editor
  • cons:
    • not convenient

Mount remote file system

  • pros:
    • you can use your own editor
    • In some mount systems like rclone you can use caching to enjoy zero latency. But rclone mount is a bit buggy overall.
  • cons:
    • In sshfs you will still suffer from latency
    • need to configure and install

Directly operate inside SSH

  • pros:
    • convenient
  • cons:
    • latency
    • need to install vim on remote machine

Now after reading vim documentation I have found the fourth way. There is actually built-in plugin in vim called netrw that can be used to edit remote files directly. For me zero-latency is very important, and it also solves the unstable problem that rclone mount has.

Netrw

  • pros:
    • convenient
    • zero latency
    • no need to install anything on remote machine
  • cons:
    • there is only a little disadvantage over remote mounting, that is, if the remote file changes, you will not automatically get reload.

How to use netrw?

According to their documentation you need to have set nocompatible and filetype plugin on in your .vimrc file. (Actually this might not be necessary).

Then you can open a remote file with vim scp://user@host/path/to/file. For example, for editing notes on asukachan.com I used

nvim scp://Asuka//opt/asukachan/notes/

And I will be able to see the directory structures. The common options you will need are

  • Enter to open a file or directory
  • o to open a file in a new window
  • v to open a file in a vertical split
  • t to open a file in a new tab
  • d to create a new directory
  • % to create a new file

You can also use :help netrw to see more options owo, the documentation is actually worth looking at!

Make things even more convenient!

You can put an alias in you ~/.bashrc file that makes editing note even easier

alias notes=nvim scp://Asuka//opt/asukachan/notes/

So you can easily launch the editor with remote connection.