Skip to content

Instantly share code, notes, and snippets.

@TripleDogDare
Last active July 13, 2023 05:24
Show Gist options
  • Save TripleDogDare/170cf93be8f4bd1f5dcbef26ad41246b to your computer and use it in GitHub Desktop.
Save TripleDogDare/170cf93be8f4bd1f5dcbef26ad41246b to your computer and use it in GitHub Desktop.
An interactive fuzzy grep showing surrounding context
#!/bin/bash
set -euo pipefail
# go get github.com/junegunn/fzf
BEFORE=10
AFTER=10
HEIGHT=$(expr $BEFORE + $AFTER + 3 ) # 2 lines for the preview box and 1 extra line fore the match
PREVIEW="$@ 2>&1 | grep --color=always -B${BEFORE} -A${AFTER} -F -- {}"
"$@" 2>&1 | fzf --height=$HEIGHT --reverse --preview="${PREVIEW}"
@TripleDogDare
Copy link
Author

This doesn't work like a normal grep command
usage:

fzgrep <command>

I primarily use this to search help documents.

fzgrep git log --help
fzgrep fzf --help

But you can searching logs is useful as well

fzgrep cat /var/log/syslog

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