Vim Tip #15: Remote Editing Files via SCP

2018-11-27(Tue)

tags: Vim

Vim Tips

Vim and NeoVim allow you to edit files remotely using the SCP protocol - this is probably my favourite Vim trick I've learned in the last year. If you don't use SSH keys for authentication, don't even think about trying this (I admit I haven't tried it without keys, but I suspect that it wouldn't be pretty). This is how you do it: nvim scp://root@router.lan//etc/hosts Note especially the double slash between the hostname and the file path: this is a protocol form I've never seen anywhere else. But once you get used to this, this is a fantastic tool.

"Why?" you may ask ... Why not just log in to the remote machine and open the file in Vim? I can think of at least a couple reasons. At work, I often deal with remote servers running odd versions of OSes I don't like, and I'm not in a position to log in and install NeoVim or my preferred plugins. If I edit the file over scp: I get all my own plugins and syntax colour schemes, which makes me a lot faster. This also applies to the example above: I'm editing a file on an OpenWRT router - they simply don't have the room to support a full (Neo)Vim-plus-plugins installation. Another place this might be useful is over an unreliable connection: the file is copied to your local machine and edited locally, the only connection to the remote is when you open the file or write it. So instead of having to deal with SSH dropping on you, you may have to deal with slow writes - but your editing session will be responsive.

Something you should stop to think about here is how this is done, and the consequences of that: this copies the file to your machine and you edit a local copy. Saves go to the remote. This isn't a bad thing, but it's not how you normally edit files and you should be aware of it.