Editing Remote Files with VIM and SCP
June 8, 2008
Two weeks ago, a good friend and I were chatting about what features would make life easier in our day to day jobs of web design and system administration. Among what we discussed was the ability to edit remote files in VIM without having to open a remote shell to the system in question. We were thinking of things like the FUSE system and Fish protocol, but weren’t aware anything was out there already. Folks on GNOME or KDE can just open a fish://, sftp://, or scp:// session in their file browser and edit directly with a GUI editor. Those of us who work mainly in command line, however, are left with simple SSH. Not that SSH is much of a hassle — It’s not. But for those times when a quick edit is all that’s necessary, wouldn’t it be nice to just reference the file directly?
As luck would have it, serendipity reared its head a couple of days ago while I was chatting with a coworker. It turns out that recent version of VIM already include this functionality! Here’s all you need to do:
$ vim scp://hostname.example.com//path/to/file.txt
Or, if you need to connect as a different user:
$ vim scp://user@hostname.example.com//path/to/file.txt
If you need to specify an alternate port, use the familiar colon (:) syntax:
$ vim scp://user@hostname.example.com:5757//path/to/file.txt
That’s all there is to it! The file is read into VIM from the remote system, if it exists. Then, you are free to make changes. When you save, changes are written to the remote system. This helps cut down on bandwidth and lag on slow connections. For more information, see the posting at vim.org.
Posted in
content rss
June 17th, 2008 at 8:55 pm
After seeing weird errors, it occured to me that second colon in the address needs to be omitted, and used it only to specify the port for the scp connection, not like scp utility,
scp://user@host/foo/bar.txt
and
scp://user@host:9922/foo/bar.txt
June 17th, 2008 at 9:56 pm
Thanks for the detail, Wunar — And actually, omitting the : isn’t all that’s necessary. Hostname should be followed by two //s. Don’t know how I missed that! Thanks!
example:
vim scp://user@host//home/user/example.txt
June 18th, 2008 at 9:47 pm
Well, it’s not exactly like it *should* be followed. It’s still
[scp://user@host/] [path] syntax, so you can use
scp://user@host/.vimrc perfectly fine, and it will take .vimrc from user’s home folder (as path = .vimrc), and scp://user@host//etc/profile will specify full path to /etc/profile.
October 20th, 2008 at 11:35 am
Has anyone used this from within Cygwin? I’m trying to edit remote files on linux servers using a Windows laptop running Cygwin.
Unfortunately, I can’t seem to get it to work. I get errors like ‘can’t open file for writing’ which seem like permission errors to me, but I don’t know at this point.
I can do this just fine from another linux machine, but not from Windows using Cygwin.
October 26th, 2008 at 5:34 pm
[[user@]host1:]file1from scp man page, sogvim scp://user@host:/etc/profilewould also work for a absolute path. Andgvim scp://user@host:.bash_profilefor a relative path.That whole u
ser@host//etc/profile, user@host/.bash_profileshould be depreciated in my opinion.February 4th, 2009 at 9:22 pm
http://www.codeislove.org/?p=10
for a potential cygwin fix.