http://www.perlmonks.org?node_id=602454

My everyday workstation runs GNOME and my editor of choice is gvim. This Perl script can integrate those two tools more tightly.

First of all, did you know that you can use gvim to edit files on remote servers over ssh like this:

gvim scp://servername//path/to/file

Second, did you know that the Nautilus file manager can browse filesystems on remote servers over ssh like this:

nautilus ssh://servername/path/to/dir

Browsing files like that is useful but when you want to edit them you discover that gvim doesn't understand the GNOME VFS URIs. With a little help from Perl, we can translate the ssh:... URIs from GNOME into scp:... URIs for gvim. Save the following script as ~/.gnome2/nautilus-scripts/Open with gvim.

#!/usr/bin/perl use strict; use warnings; my @files; foreach (split /\n/, $ENV{NAUTILUS_SCRIPT_SELECTED_URIS}) { if(s{^file://(/.*)$}{$1}) { push @files, unescape($_); } elsif(s{^ssh://([^/]+)/(.*)$}{scp://$1//$2}) { push @files, unescape($_); } else { system(qq{gdialog --infobox "Unknown URI type: '$_'"}); exit; } } my $msg = "Files:\n" . join("\n", @files); #system(qq{gdialog --infobox "$msg"}); system('gvim', @files); exit; sub unescape { my($data) = $_; $data =~ s/%([\da-f][\da-f])/chr(hex($1))/eg; return $data; }

Once you've saved the script and made it executable, if you right-click on a file in Nautilus, you'll find a 'Scripts' sub-menu with the 'Open with gvim' option. You'll find it works for local and remote files.

Both Nautilus and gvim support other protocols (such as ftp:...). You should find it pretty easy to hack in URI translations for those that you need.

Replies are listed 'Best First'.
Re: Nautilus helper script to "Open in gvim"
by Anonymous Monk on May 02, 2007 at 08:30 UTC
    Thanx. What a Cool Tip! I use Korean(Unicode) and it does pretty well with Nautilus and Vim! But may need little chage ;-)
    from: $data =~ s/%([\da-f][\da-f])/chr(hex($1))/eg; to: $data =~ s/%([\da-fA-F][\da-fA-F])/chr(hex($1))/eg;
    http://perlmonks.org/index.pl?node_id=109
Re: Nautilus helper script to "Open in gvim"
by Anonymous Monk on Oct 27, 2007 at 22:06 UTC
    Nice script. If I edit a file owned by root, gnome-ssh-askpass keeps asking me for the root password. Is there anyway to get around this or do I have to continually enter the root password every time I save a file?

      I'm not quite sure how you'd end up being prompted for the remote machine's root password unless you're browsing a URL like ssh://root@servername/. If that is the case then you could add your SSH public key (eg: ~/identity.pub) to the remote root user's ~/.ssh/authorized_keys file.

Re: Nautilus helper script to "Open in gvim"
by Anonymous Monk on Dec 13, 2007 at 06:33 UTC
    Life saver!
    I've been trying to get GVim to work with Nautilus for hours and this script worked!
    I got the error:
    'Use of uninitialized value in split at Open with gvim line 8.'
    So I had to run the following command before it worked:
    export NAUTILUS_SCRIPT_SELECTED_URIS="~/.gnome2/nautilus-scripts"
    Thanks again
    Steve Richards
Re: Nautilus helper script to "Open in gvim"
by Anonymous Monk on Jul 11, 2008 at 16:10 UTC
    Try GVFS (not Gnome-VFS). With it any app can access files on gnome mounted file systems. Even from command line.
Re: Nautilus helper script to "Open in gvim"
by Anonymous Monk on Aug 05, 2009 at 14:38 UTC
    What a cool script! Thanks from Turkey, Ozhan