Overview
Thegrep tool provides fast content search using ripgrep with regex pattern matching. Use this when you need to find files containing specific patterns.
Source: src/tools/grep/
Implementation
Uses ripgrep (rg) with:
- 60 second timeout
- 256KB output limit
- Full regex syntax support
- Multiple output modes
- File pattern filtering
Parameters
The regex pattern to search for in file contentsExamples:
"function authenticate"- Literal string"log.*Error"- Regex pattern"function\s+\w+\("- Function declarations"TODO|FIXME"- Multiple patterns"^import .* from"- Line-anchored pattern
.- Any character*- Zero or more+- One or more?- Zero or one^- Line start$- Line end\s- Whitespace\w- Word character[abc]- Character class(a|b)- Alternation
File pattern to include in the searchExamples:
"*.js"- JavaScript files only"*.{ts,tsx}"- TypeScript files"test/**/*.js"- Test files only
The directory to search inDefaults to current working directory.Example:
"src/components"Output format
"files_with_matches"- Only file paths (default, fastest)"content"- Matching lines with context"count"- Match counts per file
"files_with_matches"Limit output to first N entries
0 or omitted means no limit.Example: head_limit: 10 returns first 10 matches.Response
Search results in requested formatfiles_with_matches mode:content mode:count mode:No matches:Error:
Output modes
files_with_matches (default)
Fastest mode. Returns only file paths:content
Returns matching lines with line numbers:count
Returns match counts per file:Usage examples
Find files containing pattern
Search with file filter
View matching lines
Count matches
Search specific directory
Regex patterns
Common patterns
Find imports
Find function definitions
Find TODO/FIXME comments
Find error handling
Find API endpoints
Safety limits
Timeout
Searches timeout after 60 seconds:Output limit
Output is limited to 256KB (about 3000 lines):head_limit or more specific patterns.
Head limit
Limit results to first N entries:Ripgrep integration
The tool uses ripgrep with mode-specific flags: files_with_matches:- Uses memory maps for fast I/O
- Respects
.gitignoreautomatically - Skips binary files
- Supports parallel search
Regex syntax
Ripgrep uses Rust regex syntax (similar to PCRE):Character classes
\d- Digit\w- Word character\s- Whitespace\D,\W,\S- Negations[abc]- Character set[^abc]- Negated set
Quantifiers
*- 0 or more+- 1 or more?- 0 or 1{n}- Exactly n{n,}- n or more{n,m}- Between n and m
Anchors
^- Start of line$- End of line\b- Word boundary
Groups
(pattern)- Capture group(?:pattern)- Non-capture group(a|b)- Alternation
Examples
Case sensitivity
By default, searches are case-sensitive:(?i) prefix for case-insensitive search.
Error handling
Invalid regex
- Unmatched parentheses
- Invalid escape sequences
- Unclosed brackets
Ripgrep not found
Permission denied
Performance tips
Use file filters
Instead of:Use files_with_matches for discovery
Instead of:Limit output
Instead of:Comparison with other tools
| Feature | grep | grep (Unix) | ag | ast-grep |
|---|---|---|---|---|
| Speed | Very fast | Slow | Fast | Fast |
| Regex | Full | Basic/Extended | Full | Structural |
| Gitignore | Auto | Manual | Auto | Manual |
| Limits | Yes | No | No | No |
| Syntax | Regex | Regex | Regex | AST patterns |
grep for speed and safety.
For structural search (syntax-aware), use ast_grep.
Implementation details
CLI execution
Executes ripgrep viarunRg() or runRgCount():
Result formatting
Results are formatted byformatGrepResult() or formatCountResult():
- Adds context headers
- Formats line numbers
- Handles empty results
- Adds truncation warnings