Skip to content

Instantly share code, notes, and snippets.

@petdance
Created February 8, 2017 21:34
Show Gist options
  • Save petdance/2c6216c2a84d2c235461dea674cfdfe8 to your computer and use it in GitHub Desktop.
Save petdance/2c6216c2a84d2c235461dea674cfdfe8 to your computer and use it in GitHub Desktop.
~/bin/lev
#!/usr/bin/perl
use warnings;
use strict;
use Cwd;
use File::Spec;
my @viminfo_candidates = glob( '~/.viminfo' );
if ( !@viminfo_candidates ) {
warn "Cannot find ~/.viminfo\n";
exit 1;
}
my $viminfo = $viminfo_candidates[0];
open( my $fh, '<', $viminfo ) or do {
warn "Can't open $viminfo: $!\n";
exit 1;
};
my $lev_raw;
while ( <$fh> ) {
if ( /^'0\s+\d+\s+\d+\s+(.+)/ ) {
$lev_raw = $1;
last;
}
}
close $fh;
if ( !defined($lev_raw) ) {
warn "Can't find last opened file in $viminfo\n";
exit 1;
}
# Now massage the string into something we want.
my $lev_canon = $lev_raw;
$lev_canon =~ s{
^ ~ # find a leading tilde
( # save this in $1
[^/] # a non-slash character
* # repeated 0 or more times (0 means me)
)
}{
$1
? (getpwnam($1))[7]
: ( $ENV{HOME} || $ENV{LOGDIR} )
}ex;
my $lev_rel = File::Spec->abs2rel( $lev_canon, getcwd() );
print "$lev_rel\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment