Permalink
Browse files

Run vim with a quickfix list, preloaded pattern, and a file list

This lets me run through the matches (:n + n or :cn) and view the list
(:buffers or :cl) any way I please.  Note that the -q option to vim
won't accept a list of files after it.
  • Loading branch information...
1 parent 8a4f179 commit c2d8ed481e97bbcd6cc74797a77a68ea47f7e444 @tsibley committed Nov 14, 2013
Showing with 17 additions and 7 deletions.
  1. +17 −7 viack
View
24 viack
@@ -1,10 +1,13 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use open qw/ :std :encoding(UTF-8) /;
use File::Basename qw(basename);
+use File::Temp qw(tempfile);
+use List::MoreUtils qw(uniq);
-my ($grabnext, @vimopts, @files);
+my ($grabnext, @vimopts, @hits);
# Extract the ack pattern to use when spawning vim
for (@ARGV) {
@@ -29,26 +32,33 @@ my $pid = open my $kid, "-|";
die "Can't fork: $!" unless defined $pid;
if ($pid) {
- @files = <$kid>;
- chomp for @files;
+ @hits = <$kid>;
+ chomp for @hits;
close $kid;
} else {
my ($via_git, $invoked_as) = basename($0) =~ /^(git-)?vi(.+)/;
my $grep = ($via_git ? undef : $ENV{VIACK_GREP}) || $invoked_as || 'ack';
- my @opts = qw( -l );
+ # ack and ag default to line numbers and headings
+ my @opts = qw( --noheading --nobreak --nocolor );
# git vigrep
if ($via_git) {
- unshift @opts, "grep", "--perl-regexp";
+ @opts = ("grep", "-H", "--perl-regexp");
$grep = "git";
}
exec $grep => @opts, @ARGV
or die "Can't exec $grep: $!";
}
-if (@files) {
- exec vim => @vimopts, "--", @files
+if (@hits) {
+ my ($tmpfh, $tmpfn) = tempfile( "viack-XXXXX", TMPDIR => 1 );
+ print { $tmpfh } "$_\n" for @hits;
+ close $tmpfh;
+
+ my @files = uniq map { /^(.+?):\d/; $1 } @hits;
+
+ exec "vim", @vimopts, "+cfile $tmpfn", "--", @files
or die "Can't exec vim. $!";
} else {
die "No matches to open.\n";

0 comments on commit c2d8ed4

Please sign in to comment.