Skip to content

Instantly share code, notes, and snippets.

@vampirechicken
Created July 5, 2011 18:01
Show Gist options
  • Save vampirechicken/93eb949e37c5f8e696f8 to your computer and use it in GitHub Desktop.
Save vampirechicken/93eb949e37c5f8e696f8 to your computer and use it in GitHub Desktop.
Simplify opening vim to a desired line number determined by 'grep -rn', perl error messages, perl debugger file:line indicators, etc.
#!/usr/bin/perl
use strict;
use warnings;
my ($file, $line_num);
if (@ARGV == 3) {
$file = $ARGV[0];
$line_num = $ARGV[2];
}
else {
my $line = $ARGV[0];
($file, $line_num) = split(/(?: line |:)/, $line, 3);
}
$line_num =~ s/\D$//;
unless ($line_num =~ qr/^\d+$/) {
die "That's not a line number!";
}
exec '/usr/bin/vim', "+${line_num}", $file;
@vampirechicken
Copy link
Author

after a 'grep -rn' over mutliple files, the output looks like:

file_name:line_number:matching_line

Often, you'll want to edit one of those lines, and you'll want your editor to jump to the desired line at startup.

$ grep -rn use .
./foo.pm:7:user strict;
./foo.pm:8:use warnings;
./foo.pm:9:use Apache;
./foo.pm:10:use Apache::Cookie;
$

copy the output line up through the line number (up to the first whitespace after the line number), and feed it to vil as a comand line parameter.

$vil ./foo.pm:7:us

vil will pull the line number off the end, and exec vim with the desired line number on the command line. I you were to type the command yourself, it would be:

$vim +7 ./foo.pm

Perl error messages (and any others) or the form:

/path/to/file.pm line 27

are now supported by vil.

Saves a few clicks and a few keystrokes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment