Nautilus Script: Edit Remote Files (SSH) with Vim

My on again, off again love affair with KDE is currently off (but that’s another story). I’m once again enjoying the simplicity of Gnome, and the elegance of Nautilus.

The ability to graphically browse remote servers via SSH is great, but I found out that trying to edit a remote file chokes Vim. Apparently the netrw module does not handle an SSH URI, but rather prefers SCP. 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.

Tags: , , , , , , ,

No comments yet.

Leave a Reply