Vai al contenuto

Ripgrep (rg) Cheatsheet

Installazione

Tabella_136_

Comandi di base

Tabella_137_

Uso avanzato

Tabella_138_

Configurazione

Ripgrep può essere configurato utilizzando un file di configurazione per impostare le opzioni di default.

Configuration File Locations

# Linux/macOS
~/.config/ripgrep/config
~/.ripgreprc

# Windows
%APPDATA%\ripgrep\config

# Set custom location via environment variable
export RIPGREP_CONFIG_PATH=/path/to/config

Sample Configuration File

# ~/.ripgreprc

# Always use smart case
--smart-case

# Always show line numbers
--line-number

# Always show column numbers
--column

# Set default context lines
--context=2

# Always search hidden files
--hidden

# Custom ignore patterns
--glob=!.git/*
--glob=!node_modules/*
--glob=!*.min.js
--glob=!*.map
--glob=!dist/*
--glob=!build/*
--glob=!target/*

# Custom colors
--colors=match:fg:yellow
--colors=match:style:bold
--colors=path:fg:green
--colors=line:fg:cyan

# Performance settings
--threads=8
--max-filesize=2M

# Output formatting
--heading
--max-columns-preview=150

Variabili ambientali

# Set config file location
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"

# Disable config file
export RIPGREP_CONFIG_PATH=""

Common Use Cases

Use Case: Finding Function Definitions

# Find function definitions in Python files
rg -t py "^def \w+\("

# Find function definitions with context
rg -t py "^def \w+\(" -A 3

# Find all class definitions in JavaScript
rg -t js "^class \w+" --heading

Use Case: Searching Through Logs

# Find errors in log files
rg -i "error|exception|fatal" /var/log/

# Find errors with timestamps and context
rg "ERROR" --glob "*.log" -C 2

# Count error occurrences per log file
rg -c "ERROR" --glob "*.log" --sort path

# Search compressed logs
rg -z "pattern" /var/log/archive/

Use Case: Code Refactoring

# Find all usages of deprecated API
rg "oldFunction\(" -t js -t ts

# Find TODO comments across codebase
rg "TODO|FIXME|HACK" --heading

# Find hardcoded credentials (security audit)
rg -i "(password|api[_-]?key|secret|token)\s*=\s*['\"]" --type-add 'config:*.{yml,yaml,json,env}' -t config

# Find imports of specific module
rg "^import.*from ['\"]module-name['\"]" -t js

Use Case: Trovare valori di configurazione

# Search for config value across all config files
rg "database_host" --type-add 'config:*.{yml,yaml,json,conf,ini,toml}' -t config

# Find environment variables
rg "export \w+=" --glob "*.sh" --glob "*.bash"

# Search in dotfiles (hidden files)
rg --hidden "pattern" ~/

Use Case: Working with Large Codebases

# Search with performance optimization
rg -j 16 --mmap "pattern" /large/codebase

# Search excluding test files
rg "pattern" -g "!**/*test*" -g "!**/*spec*"

# Search only recently modified files (combine with find)
find . -type f -mtime -7 -print0 | xargs -0 rg "pattern"

# Search with file size limit
rg --max-filesize 500K "pattern" /large/codebase

Migliori Pratiche

  • Utilizza filtri di tipo file (-t, -T) invece di schemi glob quando possibile per migliorare le prestazioni e la leggibilità
  • Leverage smart case (INLINE_CODE_69__) come predefinito - cerca caso insensibile quando il modello è minuscolo, caso-sensibile altrimenti
  • Crea un file di configurazione .ripgreprc per impostare i valori predefiniti ed evitare bandiere di riga di comando ripetitive
  • Utilizzare i file .ignore_ nelle radici del progetto per escludere le directory come node_modules, dist________________________________________________________________________________________________________________________________________________________________________________________________
  • Preferisci la ricerca letterale (INLINE_CODE_75_) quando cerchi stringhe esatte contenenti metacaratteri regex per evitare di sfuggire
  • Utilizza --stats durante l'ottimizzazione per comprendere le prestazioni di ricerca e identificare i colli di bottiglia in grandi codebases
  • Combinare con altri strumenti come xargs, fzf_, o sed__ per potenti pipeline di elaborazione del testo
  • **Utilizza --files prima ** quando si debug per verificare quali file verranno ricercati prima di eseguire la ricerca effettiva
  • Set thread count (INLINE_CODE_81_) basato sui core della CPU per prestazioni ottimali su grandi ricerche
  • Utilizza la modalità multilinea (-U) con parsimonia più lenta; preferi più ricerche quando possibile

Risoluzione dei problemi

Tabella_139_

Riferimento: schemi comuni

Pattern Description Example
INLINE_CODE_109 Word boundaries INLINE_CODE_110
INLINE_CODE_111 Start of line INLINE_CODE_112
INLINE_CODE_113 End of line INLINE_CODE_114
INLINE_CODE_115 OR operator INLINE_CODE_116
INLINE_CODE_117 Digit repetition INLINE_CODE_118
INLINE_CODE_119 Case insensitive flag INLINE_CODE_120
INLINE_CODE_121 Named capture group INLINE_CODE_122
INLINE_CODE_123 Any characters INLINE_CODE_124
INLINE_CODE_125 Whitespace INLINE_CODE_126
[^)] Classe di carattere negativo rg "function\([^)]*\)"