Nautilus Script: Edit Remote Files (SSH) with Vim

My on again, off again love affair with “KDE (K Desktop Environment)”:kde is currently off (but that’s another story). I’m once again enjoying the simplicity of “Gnome (Gnome Desktop)”:gnome, and the elegance of “Nautilus (Nautilus file manager)”:naut.

The ability to graphically browse remote servers via SSH(secure shell) is great, but I found out that trying to edit a remote file chokes “Vim (Vim editor)”:vim. Apparently the netrw module does not handle an SSH URI(Uniform Resource Identifier), but rather prefers SCP(secure copy). So a quick nautilus-script to the rescue.

#!/usr/bin/env python

import os, sys
import re
import urllib

files = []

def unescape(uri):
    return re.sub(r'\s', r'\\ ', urllib.unquote(uri))

for file in os.getenv("NAUTILUS_SCRIPT_SELECTED_URIS", None).split("\n"):

    if re.match(r'^file://', file):
        files.append(unescape(re.sub(r'^file://', r"", file)))

    elif re.match(r'^ssh://', file):
        files.append(unescape(re.sub(r'^ssh://([^/]+)(/[^/]+){2}/(.*)$', r'scp://\1/\3', file)))

    else:
        pass

if files:
    os.system("""gvim """ + " ".join(files))

sys.exit(0)

Still some work to do, but for now it’s simple and effective.

[kde]http://www.kde.org/
[gnome]http://www.gnome.org/
[naut]http://www.gnome.org/projects/nautilus/
[vim]http://www.vim.org/