ripgrep (rg)

repository·master·Indexed 13 days ago

https://github.com/BurntSushi/ripgrep

A high-performance, line-oriented search tool that recursively searches directories for regex patterns. Designed for code search, it features automatic filtering of ignored and binary files, support for PCRE2, and the ability to search compressed files. Version 15.2.0.

Tokens
5.9K
Snippets
26
Records
29
Agent score
94%

What's inside ripgrep

  1. Enable long path support on Windows

    master

    For MSVC builds on Windows, ripgrep is configured to be longPathAware. This allows the tool to handle file paths that exceed the standard 260-character limit (e.g., paths starting with C:\ that are longer than 260 characters).

    This setting is applied via a Windows manifest that is linked into the final binary during the build process using build.rs.

  2. How ripgrep filtering works

    master

    By default, ripgrep is optimized for code search and applies automatic filtering to reduce noise and increase speed:

    • Respects ignore rules: It honors .gitignore, .ignore, and .rgignore files.
    • Skips hidden files: It does not search files or directories that start with a dot (e.g., .git).
    • Skips binary files: It automatically avoids searching binary data.

    To disable all automatic filtering and search everything, use the flag rg -uuu.

  3. Understand ripgrep's automatic filtering

    master

    By default, ripgrep ignores several types of files and directories to provide relevant results. It ignores:

    1. Gitignore globs: Patterns in .gitignore files (including global and repo-specific rules).
    2. Ignore files: Patterns in .ignore files (which take precedence over .gitignore).
    3. Ripgrep ignore files: Patterns in .rgignore files (which take precedence over .ignore).
    4. Hidden files: Files and directories starting with a dot (e.g., .config).
    5. Binary files: Any file containing a NUL byte.
    6. Symbolic links: ripgrep does not follow symlinks by default.
  4. Perform basic searches with ripgrep

    master

    ripgrep (rg) searches files for patterns line by line. By default, it prints the matching line along with its line number. You can search using literal strings or regular expressions.

    Literal search: Pass the text directly as the pattern.

    Regular expression search: Pass a regex pattern. Note that special characters like ( must be escaped with a backslash or you can use the -F flag to treat the pattern as a literal string.

    # Search for a literal string in a specific file
    $ rg fast README.md
    
    # Search using a regular expression
    $ rg 'fast\w+' README.md
    
    # Search using a literal string pattern (ignoring regex special characters)
    $ rg -F 'fn write('
  5. Build ripgrep from source

    master

    To build ripgrep, you need a Rust installation (version 1.96.0 or newer).

    To build with optional PCRE2 support (enabling look-around and backreferences), enable the pcre2 feature. This will attempt to link against your system's PCRE2 library or build it from source.

    To build a fully static executable on Linux, use the x86_64-unknown-linux-musl target.

    # Standard release build
    git clone https://github.com/BurntSushi/ripgrep
    cd ripgrep
    cargo build --release
    
    # Build with PCRE2 support
    cargo build --release --features 'pcre2'
    
    # Build a static MUSL executable
    rustup target add x86_64-unknown-linux-musl
    cargo build --release --target x86_64-unknown-linux-musl
  6. Use .ignore and .rgignore to whitelist files

    master

    If you want to keep a directory in your .gitignore but want ripgrep to search it, you can create an .ignore or .rgignore file. These files use glob patterns to override gitignore rules.

    Precedence order (highest to lowest):

    1. .rgignore
    2. .ignore
    3. .gitignore

    Example: If .gitignore contains log/, you can create a .ignore file in the same directory containing !log/ to ensure ripgrep searches that directory.

    # .gitignore
    log/
    
    # .ignore
    !log/
  7. Perform recursive directory searches

    master

    ripgrep searches recursively through the current working directory by default if no path is provided. You can also specify a particular directory to limit the scope of the search.

    # Search the current directory recursively
    $ rg 'pattern'
    
    # Search a specific directory recursively
    $ rg 'pattern' src/
    
    # Equivalent to searching the current directory
    $ rg 'pattern' ./
  8. Install ripgrep (rg)

    master

    ripgrep (binary name rg) can be installed via various package managers depending on your operating system. Precompiled binaries are also available for Windows, macOS, and Linux on the GitHub releases page.

    # macOS (Homebrew)
    brew install ripgrep
    
    # Windows (Chocolatey)
    choco install ripgrep
    
    # Windows (Scoop)
    scoop install ripgrep
    
    # Windows (Winget)
    winget install BurntSushi.ripgrep.MSVC
    
    # Arch Linux
    sudo pacman -S ripgrep
    
    # Fedora
    sudo dnf install ripgrep
    
    # Ubuntu/Debian
    sudo apt-get install ripgrep
    
    # Nix
    nix-env --install ripgrep
    
    # Rust (Cargo)
    cargo install ripgrep
  9. Optimize preprocessor performance with `--pre-glob`

    master

    To avoid the performance penalty of running a preprocessor on every file in a repository, use the --pre-glob flag. This tells ripgrep to only invoke the specified preprocessor when the file path matches the provided glob pattern.

    Example comparison:

    • rg --pre pre-rg 'pattern' (Slow: runs pre-rg on every file)
    • rg --pre pre-rg --pre-glob '*.pdf' 'pattern' (Fast: only runs pre-rg on .pdf files)
    time rg --pre pre-rg --pre-glob '*.pdf' 'fn is_empty' -c
  10. Run fuzz tests

    master

    To execute a specific fuzz target, use cargo fuzz run <target>.

    By default, the test will run indefinitely. To limit the execution time, pass the -max_total_time=<num seconds> flag after a double dash -- to pass the argument to the underlying fuzzer.

    Behavior on completion:

    • Success: If the test completes without finding an error, it will report how many tests were run successfully.
    • Failure: If an error is produced, the test will abort, return a non-zero error code, and display the arbitrary input that caused the failure.
    # Run a target indefinitely
    cargo fuzz run <target>
    
    # Run a target for 5 seconds
    cargo fuzz run <target> -- -max_total_time=5
  11. Use a preprocessor with `--pre`

    master

    You can use the --pre flag to run a command on every file before ripgrep searches it. This is useful for searching non-text formats like PDFs by converting them to text on the fly.

    Warning: If the preprocessor is applied to every file, it can significantly slow down searches due to the overhead of launching a new process for every single file. To mitigate this, use --pre-glob to restrict the preprocessor to specific file patterns.

    # Example: Using a preprocessor to search a PDF
    time rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf -c