#!/usr/bin/env zsh # ── ripgrep.zsh : the "better grep" layer — rg for searching INSIDE files ───── # find/fd answer "where is the file"; grep/rg answer "which files contain TEXT". # # Real /usr/bin/grep is left ALONE — pentest-tools.zsh & friends call it with # BSD-grep semantics. We add rg helpers, never shadow grep. # # rg search text recursively (uses ~/.ripgreprc defaults) # rgi / rgs force case-Insensitive / case-Sensitive # rgw whole-word match only # rgl list FILES that match (names only, no lines) # rgc count matches per file # rgt restrict to a file type: rgt py 'def main' # rgall search EVERYTHING incl .gitignore'd + hidden # rgf search by FILE NAME (rg --files | rg) — quick path filter # grephelp / search cheat card: grep vs rg side by side # point rg at the global config (only if the file exists) [[ -r "$HOME/.ripgreprc" ]] && export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc" if command -v rg >/dev/null 2>&1; then alias rgi='rg --ignore-case' alias rgs='rg --case-sensitive' alias rgw='rg --word-regexp' alias rgl='rg --files-with-matches' alias rgc='rg --count' alias rgt='rg --type' # rgt py 'pattern' alias rgall='rg --no-ignore --hidden' # incl ignored + hidden alias rgtypes='rg --type-list' # what -t names exist # rgf: filter the file LIST by name (not contents). rgf toybox rgf() { rg --files | rg --color=always "${*:?usage: rgf }"; } fi # ── grephelp / search : grep ↔ rg translation card ─────────────────────────── grephelp() { print -P "" print -P "%F{#9ccfd8}▰▰▶ GREP / RIPGREP CHEAT SHEET%f" print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f" print -P "%F{#f6c177} the idea%f find=file NAMES · grep/rg=text INSIDE files" print -P "%F{#f6c177} rg (easy mode — recursive by default)%f" print -P " %F{#e0def4}rg toybox%f text 'toybox' in every file under ." print -P " %F{#e0def4}rg toybox ~/Downloads%f ...only under Downloads" print -P " %F{#e0def4}rgw main%f whole word 'main' (not 'domain')" print -P " %F{#e0def4}rgl TODO%f just the FILE NAMES that contain TODO" print -P " %F{#e0def4}rgt py 'def '%f only .py files" print -P " %F{#e0def4}rgall secret%f include .gitignore'd + hidden files" print -P " %F{#e0def4}rg -A3 -B1 error%f 3 lines After, 1 Before each match" print -P "%F{#f6c177} grep (classic / portable — note BSD grep, no -P)%f" print -P " %F{#e0def4}grep -rn toybox .%f -r recurse, -n line numbers" print -P " %F{#e0def4}grep -rin toybox .%f add -i for case-insensitive" print -P " %F{#e0def4}grep -rl toybox .%f list matching files only" print -P " %F{#e0def4}ps aux | grep ssh%f filter a PIPE (grep's daily job)" print -P "%F{#f6c177} interactive%f %F{#e0def4}frg %f fuzzy picker, Enter opens at the line" print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f" print -P "%F{#908caa} defaults live in %f%F{#e0def4}~/.ripgreprc%f%F{#908caa} (smart-case, hidden, skips .git/node_modules)%f" print -P "%F{#908caa} rg recurses with no path; grep needs %f%F{#e0def4}-r%f%F{#908caa} and a path%f" } alias search='grephelp' # frg -- the interactive picker the cheat card above advertises. It was named # in the help text but never defined anywhere, so `frg` was command-not-found. # rg feeds fzf; Enter opens the hit at its line in $EDITOR. frg() { (( $+commands[rg] && $+commands[fzf] )) || { print -u2 "frg: needs rg and fzf"; return 1 } [[ -n $1 ]] || { print -u2 "usage: frg "; return 1 } local result file line result=$(rg --line-number --no-heading --color=always --smart-case -- "$@" 2>/dev/null | fzf --ansi --delimiter : \ --preview 'bat --style=numbers --color=always --highlight-line {2} {1} 2>/dev/null || sed -n {2}p {1}' \ --preview-window 'up,60%,border-bottom,+{2}+3/3') || return [[ -n $result ]] || return file=${result%%:*}; line=${${result#*:}%%:*} ${EDITOR:-nvim} +"$line" -- "$file" }