VIM and editing files on remote servers
An article on how to set up vim in one place, and load and work locally with all files that are on other servers. It is clear that you can put vim on each remote server, configure and work via ssh, but this is not always possible, if only because everything quickly becomes outdated and in order to use super-modern vim with all the plugins and bells and whistles, sometimes you need to replace the OS on the remote host.
I’ve put together some ways here (not all of them) with which you can edit files located on remote machines, while using your locally configured and combed vim with all the goodies.
1.scp
You need to set up authorization by key, and then you can edit files located anywhere using the command:
vim scp://user@myserver[:port]//path/to/file.txt
Or directly on the vim command line:
:e scp://user@myserver[:port]//path/to/file.txt
In addition to scp, there are other similar options. You can enter :h netrw-nread
and get help in nvim:
Pluses are clear – it is not necessary to put anything. Entered the command and edit the file. For example, if you need to edit a single configuration file, such as /etc/nginx/nginx.conf
then this is a normal way, but it is rather problematic to conduct large projects.
Minuses:
If the project is large, then you can get tired of moving in this way or looking for something there
LSP, fzf with fuzzy search and some other plugins won’t work
An example when a file is uploaded via scp. Screenshot with messages from LSP.
2.sshfs
With sshfs you can mount a remote file system and work with it locally.
Installation is required only where your vim is located.
yum install sshfs
or
apt-get install sshfs
And then we mount it in the desired folder like this:
sshfs remote-user@remote-server.com:/home/remote-user/ ~/mnt/remote-user
And we get the remote file system in our folder ~/mnt/remote-user
If you start vim and start working, then LSP will continue to swear, and you need to make some changes. For example, in order for LSP to work with pyright, you need to go to the folder ~/mnt/remote-user
put the file pyrightconfig.json
with the following content:
{
"venvPath": ".",
"venv": "env"
}
Where we explicitly specify the path to the virtual environment and the name of the folder with the virtual environment.
To unmount, here is the command:
fusermount -u ~/mnt/remote-user/
The plus is obvious, you don’t need to download anything for yourself, but simply attaches the remote file system to a local folder.
Minuses:
Very slow
When LSP accesses remote modules to read, it mercilessly dumbs down
3. samba+vpn
I will not dwell on how to set up samba with vpn in detail. For me, for a lazy person, this way is not very good, because I’m too lazy to install all this and configure it, both locally and remotely.
But about the fact that such an option exists, it still makes sense to write. The internet is full of guides on how to do this. But even in this case people complain (maybe they don’t know how to tune), which dulls their LSP. Infa for those who decide to go this way.
4. distant
There is a library called distantwhich gives access to a remote file system and even allows you to execute commands remotely. Distant is fast, asynchronous, encrypted, written in rust and everything is so cool and modern. And now, based on this library, it is written plugin for nvim.
Here is a video where a person shows that he does not have go installed locally, starts, mounts a remote host in nvim and LSP works with go at the same time.
Can work with Windows (I have not tried).
Plugin in mode mode=ssh
can work without the distant library installed, it is already slower, search, LSP, etc. will not work.
Minuses:
Distant requires a modern OS to fully run. And if the OS is modern, then it seems like it’s easier to put nvim on it with all the personal belongings and work than to mess around with extra plugins. Such a double-edged sword.
5. vim-arsync
plugin vim-arsync to asynchronously sync remote and local files using rsync. Because rsync is a thing that works on both old and new servers, there will be no problem if you have to work on a server that you configured, for example, in zero, and which has been working ever since.
Key features:
Synchronization with rsync (with compression options -> -avzhe ssh)
Can ignore certain files or folders based on the config file
Asynchronous work
It is possible to configure a file for each project
Automatic synchronization when saving a file
Works with ssh keys (recommended) or open password in config file
Minuses:
This plugin pulls the entire project with all the files from the remote machine to the local one. But then you can work very quickly. In short, the minus is only in the disk space that must be given to the project.