Skip to content

Instantly share code, notes, and snippets.

@SmallJoker
Created December 30, 2024 19:14
Show Gist options
  • Save SmallJoker/0115681ac5f2394973af5d0bc49eb1be to your computer and use it in GitHub Desktop.
Save SmallJoker/0115681ac5f2394973af5d0bc49eb1be to your computer and use it in GitHub Desktop.
grep + xdg-open to open files based on text contents from your terminal
#!/usr/bin/env bash
# "xdg-grep" script for $HOME/.local/bin/
set -e
if [ -z "$1" ]; then
echo "Missing keyword"
exit 0
fi
where="$PWD"
lines=$(grep -r "$1" "$where" --files-with-matches --binary-files=without-match)
count=$(echo "$lines" | wc -l)
echo "---- Results ----"
if [ "$count" -gt 20 ]; then
echo "(too many results: ${count})"
exit 0
fi
if [ "$count" -gt 3 ]; then
echo "$lines"
exit 0
fi
while read -r filename; do
echo "$filename"
xdg-open "$filename"
done <<< "$lines"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment