#!/usr/bin/env zsh
# ── grep.zsh : the classic/portable text searcher — GNU grep helpers ─────────
# rg is the daily driver (see ripgrep.zsh + `search`). grep is what's EVERYWHERE
# — every box, every pipe, every script — so it's worth knowing cold. This adds
# short `grep*` verbs and a full grep-only cheat card. It NEVER touches `grep`
# itself: pentest tools call real /usr/bin/grep with their own flags, so we only
# add NEW names. (`grepr`, not a `grep` alias.)
#
# grepr <t> recursive + line numbers, skips binary (the everyday one)
# grepri <t> ...case-insensitive
# grepl <t> just the FILE names that match grepc count per file
# grepo <re> print only the matching text grepctx ±3 lines context
# grepe / grepp extended-regex (a|b) / Perl-regex (lookahead)
# grepx <t> recursive but skip .git/node_modules/venv junk
# grep-cheat / grepcheat this card · search grep↔rg translation
if command -v grep >/dev/null 2>&1; then
# -I skip binary files, -n line numbers, -r recurse. The workhorse set.
alias grepr='grep -rIn'
alias grepri='grep -rIin'
alias grepl='grep -rIl' # names of files that match
alias grepc='grep -rIc' # count matches per file
alias grepo='grep -o' # print only the matched substring
alias grepe='grep -E' # extended regex: grepe 'foo|bar'
alias grepp='grep -P' # Perl regex: grepp '(?<=id=)\d+'
alias grepctx='grep -rIn -C3' # 3 lines of context around each hit
# skip the usual noise dirs (brace-expands interactively)
alias grepx='grep -rIn --exclude-dir={.git,node_modules,.venv,venv,__pycache__,dist,build}'
fi
grepcheat() {
print -P ""
print -P "%F{#9ccfd8}▰▰▶ GREP CHEAT SHEET%f %F{#908caa}the portable text searcher (GNU grep)%f"
print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f"
print -P "%F{#f6c177} shape%f %F{#e0def4}grep [flags] PATTERN [file…]%f · no file? reads stdin (pipes)"
print -P "%F{#f6c177} the everyday recursive search%f"
print -P " %F{#e0def4}grep -rIn foo .%f recurse (-r), skip binary (-I), line #s (-n)"
print -P " %F{#e0def4}grepr foo .%f ↑ same, aliased"
print -P " %F{#e0def4}grepri foo .%f add -i, case-insensitive"
print -P " %F{#e0def4}grepx foo .%f skip .git/node_modules/venv/build noise"
print -P "%F{#f6c177} what comes back%f"
print -P " %F{#e0def4}-l%f files that match %F{#e0def4}-L%f files that DON'T %F{#e0def4}-c%f count per file"
print -P " %F{#e0def4}-o%f only the match %F{#e0def4}-n%f line # %F{#e0def4}-b%f byte offset"
print -P " %F{#e0def4}-h%f hide filenames %F{#e0def4}-H%f force filenames"
print -P "%F{#f6c177} matching%f"
print -P " %F{#e0def4}-i%f ignore case %F{#e0def4}-w%f whole word %F{#e0def4}-x%f whole line %F{#e0def4}-v%f INVERT (non-matching)"
print -P " %F{#e0def4}-F%f fixed string (literal, fast) %F{#e0def4}-E%f extended re %F{#e0def4}-P%f Perl re"
print -P " %F{#e0def4}grep -E 'foo|bar'%f OR %F{#e0def4}grep -P '(?<=id=)\\d+'%f lookbehind"
print -P " %F{#e0def4}-m3%f stop after 3 hits %F{#e0def4}-a%f treat binary as text"
print -P "%F{#f6c177} context (like a diff)%f"
print -P " %F{#e0def4}-A3%f 3 lines After %F{#e0def4}-B2%f 2 Before %F{#e0def4}-C3%f 3 both sides"
print -P "%F{#f6c177} scope the recursion%f"
print -P " %F{#e0def4}--include='*.py'%f only these %F{#e0def4}--exclude='*.min.js'%f skip these"
print -P " %F{#e0def4}--exclude-dir=.git%f prune a dir %F{#e0def4}-R%f recurse + follow symlinks"
print -P "%F{#f6c177} the pipe job (grep's other half of life)%f"
print -P " %F{#e0def4}ps aux | grep -i ssh%f %F{#e0def4}dmesg | grep -i usb%f"
print -P " %F{#e0def4}… | grep -v grep%f drop grep's own line from ps output"
print -P " %F{#e0def4}… | grep --line-buffered X%f keep matches flowing in a live pipe"
print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f"
print -P "%F{#908caa} recurse needs %f%F{#e0def4}-r%f%F{#908caa} + a path; rg recurses by default. See %f%F{#e0def4}search%f%F{#908caa} for grep↔rg.%f"
}
alias grep-cheat='grepcheat'