Overview
The Grep tool searches file contents using regular expressions and returns matching files with line numbers and context. Results are sorted by modification time (newest first) to prioritize recently changed files.Parameters
The regex pattern to search for in file contents. Use literal_text=true for exact text matching.
The directory to search in. Defaults to the current working directory.
File pattern to include in the search (e.g.,
*.js, *.{ts,tsx}). Only searches files matching this pattern.If true, treats the pattern as literal text with special regex characters escaped. Recommended for non-regex users. Defaults to false.
Features
Regex Support
Whenliteral_text: false (default), full regex syntax is supported:
.- Any character*- Zero or more of previous+- One or more of previous?- Zero or one of previous[]- Character class()- Grouping|- Alternation^- Start of line$- End of line\s- Whitespace\w- Word character\d- Digit
Literal Text Mode
Setliteral_text: true to search for exact text:
function() instead of treating it as a regex.
File Filtering
Useinclude to limit which files are searched:
*.js- JavaScript files*.{ts,tsx}- TypeScript files*.go- Go files*.{py,pyw}- Python files*test*- Test files
Ignore Support
Grep automatically respects:.gitignorepatterns.crushignorepatterns
Performance
Fast search with ripgrep:- Uses ripgrep (rg) if available on the system
- Falls back to Go implementation if ripgrep not found
- Searches are parallelized for speed
- Maximum 100 matching files returned
- Results sorted by modification time (newest first)
- Truncation notice shown if limit exceeded
Output Format
Results show file paths with matching lines:- File path (slash-separated for cross-platform compatibility)
- Line number where match occurs
- Character position (1-based) of the match
- Line content (truncated to 500 characters if longer)
Usage Examples
Simple Text Search
Regex Pattern Search
Literal Text with Special Characters
Filter by File Type
Search in Specific Directory
Regex Pattern Examples
Find Function Definitions
Find Import Statements
Find Error Handling
Find TODO Comments
Find Function Calls
Best Practices
Effective Searching
- Start broad, then narrow - Begin with simple patterns, add specificity
- Use include patterns - Filter by file type to reduce noise
- Check truncation notice - Refine search if results are truncated
- Combine with Glob - Use Glob to find files, Grep to search contents
Pattern Design
- Test patterns - Start simple and add complexity
- Escape special characters - Or use
literal_text: true - Use anchors -
^and$for line boundaries - Consider context - Include surrounding text in pattern
Performance
- Be specific - More specific patterns = faster results
- Use file filters - Searching fewer files = faster
- Avoid . at start* - Leading wildcards are slow
- Consider search scope - Smaller directories = faster
Common Patterns
Code Patterns
Configuration Patterns
Documentation Patterns
Permissions
Grep does not require permissions when:- Searching within the working directory
- Reading text files
Cross-Platform Notes
File Paths
Results use forward slashes/ on all platforms:
- Windows:
C:/Users/name/file.txt - Unix:
/home/user/file.txt
Hidden Files
Hidden files (starting with.) are skipped by default on all platforms, matching ripgrep behavior.
Line Endings
Both Unix (LF) and Windows (CRLF) line endings are handled transparently.Troubleshooting
No Matches Found
- Check pattern syntax
- Try simpler pattern first
- Verify file type with include pattern
- Check if files are in .gitignore
- Use View to verify file contents
Too Many Results
- Make pattern more specific
- Use include to filter file types
- Specify a narrower search path
- Add context to pattern
Pattern Errors
- Check regex syntax
- Escape special characters
- Use
literal_text: truefor exact text - Test pattern in isolation
Performance Issues
- Narrow search scope with path
- Use include to filter files
- Make pattern more specific
- Avoid leading wildcards
- Install ripgrep for faster search
Grep vs Other Tools
Grep vs View
Use Grep when:- You need to find files containing text
- You’re searching across many files
- You know what pattern to look for
- You want to read entire files
- You need to see file structure
- You need context around code
Grep vs Glob
Use Grep when:- Searching file contents
- Pattern matching text inside files
- Finding specific code patterns
- Finding files by name
- Searching by file path patterns
- File system organization
Grep vs Bash grep
Use Grep tool:- Respects .gitignore automatically
- Sorted by modification time
- Integrated into Crush workflow
- Cross-platform consistent
- Complex shell pipelines
- When combined with other commands
- Custom output formatting