Using vim and scp to Edit Remote Files

created Feb 13, 2020

I learned something new this week that I probably should have known long ago, although I'm unsure how often I would have used this feature.

I did not know that it was possible to edit remote files with vim.

Feb 9, 2020 Hacker News thread that contains nearly 400 comments.

"My productivity app for the past 12 years has been a single .txt file"

https://jeffhuang.com/productivity_text_file

https://news.ycombinator.com/item?id=22276184

The discussion about using ONE text file to collect notes over many years is interesting. Commenters provided their experiences and tools and hacks, such as the following.

I handle this by aliasing todo to vim scp://[my website]/~/todo.txt.

Reply:

Wait, you can use scp as a protocol?!

Another HN comment:

... but vim can: https://vim.fandom.com/wiki/Editing_remote_files_via_scp_in_vim

Reply to that comment:

wow, that's incredible. learn something new everyday.

Another comment:

I've found that VIM 8's SCP does not support absolute or realpaths like SCP generally does. Thus to access the remote file at foo:/home/dotan/todo.txt or foo:~/todo.txt I would have to use vim scp://dotan@foo/todo.txt. Just a heads-up for anybody who wishes to try this.

Other users rely on Emacs to edit remote files. Some day, I'll attempt to learn Emacs. I have used vi/vim for nearly 25 years.

HN comment:

The subsystem in Emacs called Tramp makes this almost transparent; you edit your remote files as if they were local. There are occasional kinks with path translation in corner-case situations, like local Emacs editing remote source files and trying to load them into remote REPL, but those are rare and can be configured away. Dired (directory manager in Emacs) also works transparently over Tramp, so this essentially replaces SCP and graphical SFTP tools for me. You can even run GDB remotely over Tramp, which is a nice trick.

Back to the vim.fandom.com post, which, apparently, was created in 2002. !!! Excerpt:

Vim 6.x has the netrw plugin installed as a standard plugin. It allows you to edit files via ftp, rcp, scp, or http. If your username differs on the remote host, however, and you're trying to use scp, things can get a little weird, particularly if you're not editing a document under your user tree.

I was able to edit a file under document root on my web server in read-only mode, since the owner of the web files that I create via my web-based CMS is different from the owner that I use to log into the web server with secure shell.

vim scp://user@domainname//home/cmsappdir/Wren/root/notes/test-post-12feb2020.txt

But I could update a file that was owned by the user that I use to log into the server.

vim scp://user@domainname//home/user/test.txt

Each time that I execute the above vim scp command, I'm prompted for the password of the user account that I mention in the above command. And when I want to save the contents, I'm prompted for the password again.

More from the vim.fandom.com post:

Notice two things:

  1. remoteuser@: This is used to specify the user name on the remote server. Without this, it will use the user's name on the local computer. Often that will come from the $USERNAME environment variable. If the user has the same name on the local computer and the remote server, this part is unnecessary. If you're unsure whether it's needed, use it just to be safe.

  2. Double slashes ("//") between the hostname and file path: At least one slash is needed to separate the remote server's hostname from the file path. That slash is not included in the path used to reference the file on the remote server. If the path to the file is absolute, then it must begin with a slash, giving two slashes between the hostname and file path as shown above.

However, if the file to be edited is contained within the home directory of the remote user, a relative path may be used, which should not use a second slash. For example, if the absolute path to the file to be edited is /users/remoteuser/relative/path/to/document and the home directory for remoteuser is /users/remoteuser, then the following command will open that file:

vim scp://remoteuser@server.tld/relative/path/to/document

Better late than never to learn potentially useful things.

Regarding the idea of using one text file for many years to store notes, here's the top HN comment:

I use a single text file, have done this for decades. I dump everything into it and don't worry too much about structure, it's an incredible resource for "remembering" minutia, things like "what was that server that i thought might have had some issues last August?" Just a quick control-S and I'm there. My current 7+ year old notes file has about 100K lines in it right now. I can tell you what I was doing last year, or the year before, on this day, in seconds.

I just type

note (maybe some text here)

and it cracks open Emacs on the notes file, adds a timestamp, appends the optional text, and then I can just type or paste something in. Nearly frictionless, that's key. Great for writing those miserable annual reviews, too (easy answers to "what have I worked on in the past year?")

I use plain old notebooks (simple blank sketchpads, or something bound like a Moleskin/Lecchturm) for meetings, interviews, walking around datacenters and so forth where a keyboard is awkward and anyway, drawings.

I've tried the EverNote/OneNote/etc. apps and none of them were as convenient or as easy to use, or as portable.

I still carry and use small, pocket-sized paper notebooks, along with a ballpoint pen or a mechanical pencil. When taking notes outside in cold weather, I use the pencil. Ditto when taking notes in a Rite in the Rain notebook when it's raining. I've tried the Fisher Space Pens in cold and rainy weather, but I prefer the pencil. Often in cold weather, my iPhone batter drains quickly, making the device useless as a note-taking tool.

-30-