Skip to content

Instantly share code, notes, and snippets.

@MatthewScholefield
Last active December 13, 2020 02:13
Show Gist options
  • Save MatthewScholefield/f66510ca7ff5818ea343ec65fdf10f8a to your computer and use it in GitHub Desktop.
Save MatthewScholefield/f66510ca7ff5818ea343ec65fdf10f8a to your computer and use it in GitHub Desktop.
PDF Searcher

Interactive PDF Searcher

Use this tool to quickly search PDFs:

asciicast

It continuously asks for a search query and then asks you for a search result to open in a PDF reader.

Installation

This tool requires the following:

  • Linux (Or WSL with X)
  • Evince PDF Reader

Install by copying executable to somewhere inside your PATH:

url=https://gist.githubusercontent.com/MatthewScholefield/f66510ca7ff5818ea343ec65fdf10f8a/raw/pdf-searcher
curl "$url" | sudo tee /usr/local/bin/pdf-searcher
sudo chmod a+x /usr/local/bin/pdf-searcher

Usage

Run pdf-searcher in a folder that contains PDFs. Search results are case insensitive.

#!/usr/bin/env bash
while echo "Enter a search:" && read -e -p "> " line; do
history -s "$line"
echo "Searching for '$line'..."
{
IFS=$'\n'
rows=$(pdfgrep -RnFi "$line" . --color=always --cache)
if [ -z "$rows" ]; then
echo "No results"
fi
select row in $rows; do
if [ -n "$row" ]; then
# Remove color
row=$(sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" <<< $row)
file=${row%%:*}
page=${row#*:}
page=${page%%:*}
( evince "$file" --page-label=$page &>/dev/null & )
fi
break
done
}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment