@@ -1941,8 +1941,8 @@ alias ngu="npm-global-update"
19411941#
19421942# Features:
19431943# - Manual conversion: g2rg "grep -ri 'pattern' ." or g2rg "-ri 'pattern' ."
1944- # - Auto- conversion: grep commands automatically use ripgrep in interactive shells
1945- # - Bypass: Use grep -og or grep --use-grep to use original grep
1944+ # - Command conversion: rgrep [ grep-args] - converts and executes grep commands using ripgrep
1945+ # - Original grep is preserved and accessible via the standard grep command
19461946#
19471947# Usage: source this file in your .zshrc
19481948
@@ -1984,7 +1984,8 @@ g2rg() {
19841984 # Split combined flags (e.g., -ri becomes -r -i)
19851985 local flags=${word#-}
19861986 for (( j=1; j<=${#flags}; j++ )); do
1987- local flag=${flags:$((j-1)):1}
1987+ local pos=$((j-1))
1988+ local flag=${flags:$pos:1}
19881989 case $flag in
19891990 r|R)
19901991 # ripgrep is recursive by default
@@ -2145,38 +2146,30 @@ g2rg-exec() {
21452146}
21462147
21472148# ============================================================================
2148- # Automatic grep interception (interactive shells only)
2149+ # rgrep function (interactive shells only)
21492150# ============================================================================
21502151
21512152if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
21522153
2153- # Store the original grep command path
2154- if [[ -z $__ORIGINAL_GREP_PATH ]]; then
2155- __ORIGINAL_GREP_PATH="$(which grep)"
2156- fi
2157-
2158- # Function to call original grep
2159- __original_grep() {
2160- "$__ORIGINAL_GREP_PATH" "$@"
2161- }
2162-
21632154 # Direct argument version of g2rg for stdin input
21642155 g2rg-direct-stdin() {
21652156 local output="rg"
21662157 local pattern=""
21672158 local glob_excludes=()
21682159 local pattern_found=false
21692160
2161+ local args=("$@")
21702162 local i=1
21712163 while [[ $i -le $# ]]; do
2172- local word="${!i }"
2164+ local word="${args[$i] }"
21732165
21742166 # Handle combined flags like -ri, -rn, etc.
21752167 if [[ $word =~ ^-[a-zA-Z]{2,}$ ]] && [[ $word != --* ]]; then
21762168 # Split combined flags (e.g., -ri becomes -r -i)
21772169 local flags=${word#-}
21782170 for (( j=1; j<=${#flags}; j++ )); do
2179- local flag=${flags:$((j-1)):1}
2171+ local pos=$((j-1))
2172+ local flag=${flags:$pos:1}
21802173 case $flag in
21812174 r|R)
21822175 # ripgrep reads stdin when no files specified
@@ -2276,16 +2269,18 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22762269 local glob_excludes=()
22772270 local pattern_found=false
22782271
2272+ local args=("$@")
22792273 local i=1
22802274 while [[ $i -le $# ]]; do
2281- local word="${!i }"
2275+ local word="${args[$i] }"
22822276
22832277 # Handle combined flags like -ri, -rn, etc.
22842278 if [[ $word =~ ^-[a-zA-Z]{2,}$ ]] && [[ $word != --* ]]; then
22852279 # Split combined flags (e.g., -ri becomes -r -i)
22862280 local flags=${word#-}
22872281 for (( j=1; j<=${#flags}; j++ )); do
2288- local flag=${flags:$((j-1)):1}
2282+ local pos=$((j-1))
2283+ local flag=${flags:$pos:1}
22892284 case $flag in
22902285 r|R)
22912286 # ripgrep is recursive by default
@@ -2392,19 +2387,12 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
23922387 echo "$output"
23932388 }
23942389
2395- # Enhanced grep override function
2396- grep () {
2390+ # Enhanced ripgrep function (use rgrep to convert grep commands to ripgrep)
2391+ rgrep () {
23972392 # Check if ripgrep is available
23982393 if ! command -v rg &> /dev/null; then
23992394 echo "Warning: ripgrep (rg) not found, falling back to original grep" >&2
2400- __original_grep "$@"
2401- return $?
2402- fi
2403-
2404- # Check if user wants to bypass auto-conversion
2405- if [[ $1 == "--use-grep" ]] || [[ $1 == "-og" ]]; then
2406- shift
2407- __original_grep "$@"
2395+ command grep "$@"
24082396 return $?
24092397 fi
24102398
@@ -2437,48 +2425,29 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
24372425 # Helper functions
24382426 # ========================================================================
24392427
2440- # Use original grep
2441- original-grep() {
2442- __original_grep "$@"
2443- }
2444-
2445- # Toggle verbose mode
2446- grep-verbose-on() {
2428+ # Toggle verbose mode for rgrep
2429+ rgrep-verbose-on() {
24472430 export GREP_TO_RG_VERBOSE=1
2448- echo "grep-to-rg verbose mode enabled"
2431+ echo "rgrep verbose mode enabled"
24492432 }
24502433
2451- grep -verbose-off() {
2434+ rgrep -verbose-off() {
24522435 unset GREP_TO_RG_VERBOSE
2453- echo "grep-to-rg verbose mode disabled"
2454- }
2455-
2456- # Disable auto-conversion
2457- grep-auto-off() {
2458- unfunction grep 2>/dev/null
2459- echo "grep auto-conversion disabled (source again to re-enable)"
2436+ echo "rgrep verbose mode disabled"
24602437 }
24612438
24622439 # Show status
2463- grep-status() {
2464- echo "g2rg Status:"
2465- echo " Auto-conversion: $(if [[ $(type -w grep) == "grep: function" ]]; then echo "enabled"; else echo "disabled"; fi)"
2440+ rgrep-status() {
2441+ echo "rgrep Status:"
24662442 echo " Verbose mode: $(if [[ -n $GREP_TO_RG_VERBOSE ]]; then echo "enabled"; else echo "disabled"; fi)"
24672443 echo " Ripgrep available: $(if command -v rg &> /dev/null; then echo "yes"; else echo "no"; fi)"
24682444 echo ""
24692445 echo "Commands:"
24702446 echo " g2rg \"command\" - Convert grep command to ripgrep"
24712447 echo " g2rg-exec \"command\" - Convert and execute"
2472- echo " grep -og - Use original grep (bypass conversion)"
2473- echo " original-grep - Use original grep"
2474- echo " grep-verbose-on/off - Toggle conversion display"
2475- echo " grep-auto-off - Disable auto-conversion"
2476- echo " grep-status - Show this status"
2448+ echo " rgrep [grep-args] - Convert grep arguments to ripgrep and execute"
2449+ echo " rgrep-verbose-on/off - Toggle conversion display"
2450+ echo " rgrep-status - Show this status"
24772451 }
24782452
2479- # Show initial notice (only once per session)
2480- if [[ -z $GREP_TO_RG_LOADED ]]; then
2481- export GREP_TO_RG_LOADED=1
2482- fi
2483-
24842453fi
0 commit comments