_
_
Ripgrep (rg) Cheatsheet¶
• Installation
| Platform | Command |
|---|---|
| Ubuntu/Debian | INLINE_CODE_8 |
| Ubuntu/Debian (latest) | INLINE_CODE_9 |
| Fedora | INLINE_CODE_10 |
| RHEL/CentOS | INLINE_CODE_11 |
| Arch Linux | INLINE_CODE_12 |
| Alpine Linux | INLINE_CODE_13 |
| macOS (Homebrew) | INLINE_CODE_14 |
| macOS (MacPorts) | INLINE_CODE_15 |
| Windows (Chocolatey) | INLINE_CODE_16 |
| Windows (Scoop) | INLINE_CODE_17 |
| Windows (Winget) | INLINE_CODE_18 |
| Snap (Universal) | INLINE_CODE_19 |
| From Source (Cargo) | INLINE_CODE_20 |
| Verify Installation | INLINE_CODE_21 |
| _ | |
| oder Grundlegende Befehle |
| Command | Description |
|---|---|
| INLINE_CODE_22 | Search for pattern in current directory recursively |
| INLINE_CODE_23 | Search for pattern in specific file |
| INLINE_CODE_24 | Search for pattern in specific directory |
| INLINE_CODE_25 | Case-insensitive search |
| INLINE_CODE_26 | Case-sensitive search (override smart case) |
| INLINE_CODE_27 | Smart case (case-insensitive if all lowercase) |
| INLINE_CODE_28 | Search for exact literal string (no regex) |
| INLINE_CODE_29 | Search for whole word matches only |
| INLINE_CODE_30 | Show only filenames containing matches |
| INLINE_CODE_31 | Show only filenames without matches |
| INLINE_CODE_32 | Count matches per file |
| INLINE_CODE_33 | Show line numbers (enabled by default) |
| INLINE_CODE_34 | Hide line numbers |
| INLINE_CODE_35 | Show column numbers of matches |
| INLINE_CODE_36 | Show 3 lines of context before and after match |
| INLINE_CODE_37 | Show 2 lines before match |
| INLINE_CODE_38 | Show 2 lines after match |
| INLINE_CODE_39 | Search hidden files and directories |
| INLINE_CODE_40 | Search all files including ignored ones |
| INLINE_CODE_41 | Search everything (hidden + ignored + binary) |
| _ | |
| / Fortgeschrittene Nutzung |
| Command | Description |
|---|---|
| INLINE_CODE_42 | Search only Python files |
| INLINE_CODE_43 | Exclude JavaScript files from search |
| INLINE_CODE_44 | Search using glob pattern |
| INLINE_CODE_45 | Exclude files matching glob pattern |
| INLINE_CODE_46 | List all supported file types |
| INLINE_CODE_47 | Define and use custom file type |
| INLINE_CODE_48 | Show only matched text, not entire line |
| INLINE_CODE_49 | Show matches with replacement text |
| INLINE_CODE_50 | Output results in JSON format |
| INLINE_CODE_51 | Multiline search mode |
| INLINE_CODE_52 | Use PCRE2 regex engine for advanced patterns |
| INLINE_CODE_53 | Read patterns from file (one per line) |
| INLINE_CODE_54 | Show search statistics after results |
| INLINE_CODE_55 | Use 8 threads for parallel search |
| INLINE_CODE_56 | Sort results by file path |
| INLINE_CODE_57 | Skip files larger than 1MB |
| INLINE_CODE_58 | Skip lines longer than 500 characters |
| INLINE_CODE_59 | Follow symbolic links |
| INLINE_CODE_60 | Don't respect .gitignore files |
| INLINE_CODE_61 | List all files that would be searched |
| INLINE_CODE_62 | Force color output for piping |
| INLINE_CODE_63 | Customize match color |
| INLINE_CODE_64 | Group results by file with headings |
| INLINE_CODE_65 | Show results inline without file grouping |
| INLINE_CODE_66 | Search for word boundary and show 5 lines after |
| _ | |
| Konfiguration |
Ripgrep kann mit einer Konfigurationsdatei konfiguriert werden, um Standardoptionen festzulegen.
Datei-Standorte konfigurieren¶
# 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
Umgebungsvariablen¶
# Set config file location
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"
# Disable config file
export RIPGREP_CONFIG_PATH=""
Häufige Anwendungsfälle
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: Suche durch 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: Konfigurationswerte finden¶
# 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: Arbeiten mit großen 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
oder Best Practices
- ** Verwenden Sie Dateityp-Filter* (
-t,-T_) anstelle von Glocke-Mustern, wenn möglich für bessere Leistung und Lesbarkeit - **Leverage smart case* (
-S_) als default - es sucht case-insensitive, wenn Muster kleiner ist, case-sensitive anderweitig - Eine
.ripgreprcconfig-Datei erstellen, um Ihre bevorzugten Standardeinstellungen festzulegen und wiederholte Kommandozeilen-Flags zu vermeiden.ignore__________________________________________________________ - **Literaturvoreinstellung* (
-F) bei der Suche nach genauen Strings, die Regex-Metazeichen enthalten, um eine Flucht zu vermeiden - Benutzen
--statswährend der Optimierung, um die Suchleistung zu verstehen und Engpässe in großen Codebasen zu identifizieren - ** Kombinieren Sie mit anderen Werkzeugen** wie
xargs, __INLINE_CODE_78_ odersedfür leistungsstarke Textverarbeitungspipelines - Use
--fileszuerst wenn debugging überprüft wird, welche Dateien vor der eigentlichen Suche gesucht werden - ** Gewindezählung** (
-j) basierend auf Ihren CPU-Kernen für optimale Leistung bei großen Suchanfragen - ** Verwenden Sie Multiline-Modus* (
-U) sparsam, da es langsamer ist; bevorzugen Sie mehrere Suchvorgänge, wenn möglich
Fehlerbehebung
| Issue | Solution |
|---|---|
| No results found but pattern exists | Check if files are being ignored; use INLINE_CODE_83 or INLINE_CODE_84 to search ignored/hidden files |
| Binary file matches not shown | Use INLINE_CODE_85 or INLINE_CODE_86 to search binary files as text, or INLINE_CODE_87 to search everything |
| Search is too slow | Reduce thread count with INLINE_CODE_88 to check if parallelism is causing issues, or use INLINE_CODE_89 to skip large files |
| Regex pattern not working | Verify regex syntax; use INLINE_CODE_90 for literal search or INLINE_CODE_91 for PCRE2 engine with advanced features |
| Colors not showing in output | Use INLINE_CODE_92 when piping to other commands, or check terminal color support |
| INLINE_CODE_93 not being respected | Ensure you're in a git repository; use INLINE_CODE_94 to see which ignore files are being used |
| Memory usage too high | Disable memory mapping with INLINE_CODE_95 or reduce INLINE_CODE_96 value |
| Results not sorted as expected | Use INLINE_CODE_97, INLINE_CODE_98, or INLINE_CODE_99 (note: disables parallelism) |
| Config file not being loaded | Check INLINE_CODE_100 environment variable and verify file exists at expected location |
| Symbolic links not followed | Use INLINE_CODE_101 or INLINE_CODE_102 to follow symbolic links during search |
| Pattern matches too much | Use word boundaries (INLINE_CODE_103), start/end anchors (INLINE_CODE_104, INLINE_CODE_105), or INLINE_CODE_106 for whole word matches |
| Can't search in specific file type | Check INLINE_CODE_107 for available types or define custom type with INLINE_CODE_108 |
| _ | |
| Schnell. Referenz: Gemeinsame Muster |
| 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 |
| _ | [^)] |
Verhandelte Zeichenklasse |