grep and ripgrep Command Builder
Build grep and ripgrep searches with include, exclude and context.
About this tool
Set the pattern, where to look, which files and folders to include or skip, and what to show (lines with context, only matches, counts or file names). Get the matching GNU grep and ripgrep commands with every flag explained, and test the pattern on sample text right in the page.
How to use it
- Type what to search for. Several lines become several patterns, any of which can match.
- Choose regex, basic, plain text or Perl syntax, and options like ignore case or whole words.
- Set where to search and which files and folders to include or skip.
- Pick what to show, test the pattern on sample text, then copy the grep or ripgrep command.
Questions
- How do I search recursively but skip some folders?
- Add --exclude-dir for each folder, for example grep -r --exclude-dir=.git --exclude-dir=vendor pattern . With ripgrep, anything listed in .gitignore is skipped automatically, and -g '!folder/' skips more.
- How do I search only certain file types?
- With grep, add --include='*.log' (quote it, so the shell doesn’t expand the star). With ripgrep, use -g '*.log' or a built-in type like -t py or -t java.
- What is the difference between grep -E and plain grep?
- Plain grep uses basic regular expressions, where + ? | ( ) are ordinary characters unless escaped with a backslash. -E switches to extended syntax, where they work as operators, just like in most programming languages.
- Should I use grep or ripgrep?
- ripgrep (rg) is much faster on large projects, searches recursively by default and respects .gitignore. grep is installed on every Unix system, which matters on servers and in scripts.
- How do I show lines around each match?
- Use -C 3 for three lines of context on both sides, or -B and -A for before and after separately. The preview here shows the same context lines.
Something not working, or a result looks wrong? Report a problem with this tool