Editing Remote Files with VIM and SCP

Date 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.

3 Responses to “Editing Remote Files with VIM and SCP”

  1. Wunar said:

    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

  2. mshade said:

    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

  3. Wunar said:

    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.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>