chris blogs

July 2013

13jul2013 · Summer of Scripts: pacgrep

Today’s shell function is for the Arch Linux users:

pacgrep() {
  PATTERN=${1:?pattern missing}
  shift
  pacman -Qlq "$@" | xargs grep -d skip -e "$PATTERN"
}
_pacgrep() {
  _pacman  # force autoload
  _arguments : '1:pattern:' '*:package:_pacman_completions_installed_packages'
}
compdef _pacgrep pacgrep

I even include command line completion for it. :)

It’s a really simple variant of g actually: it limits the search to files belonging to the given Arch Linux packages. E.g. when I tried to figure out where lhs2TeX mangled my >> in the files, I can search for it:

% pacgrep '>>' lhs2tex
Binary file /usr/bin/lhs2TeX matches
Binary file /usr/share/doc/lhs2tex/doc/Guide2.pdf matches
/usr/share/lhs2tex-1.18.1/lhs2TeX.fmt:%format >>         = "\sequ "
/usr/share/lhs2tex-1.18.1/lhs2TeX.fmt:%format >>=        = "\bind "

Or, I can get a list of Perl scripts included with Git:

% pacgrep bin/perl git
/usr/bin/git-cvsserver:#!/usr/bin/perl
/usr/lib/git-core/git-add--interactive:#!/usr/bin/perl
/usr/lib/git-core/git-archimport:#!/usr/bin/perl
/usr/lib/git-core/git-cvsexportcommit:#!/usr/bin/perl
...

This function is often useful for finding where error messages come from or which internal files are used by the package, without knowing where they are.

NP: Canal Terror—Staatsfeind

Copyright © 2004–2016