ripgrep


Regex | Recursive search | Automatic filtering | Globbing | Using Type Lists | File encoding | Common options



Favorites

  • Search hidden, insensitive, fixed strings
rg -.iF 'fn write(' ripgrep-0.7.1
  • Search hidden, insensitive, zip and, binary files
rg -.iza
  • Count matches per file, insensitive, fixed strings
rg -ciF 'fn write(' ripgrep-0.7.1

Search ripgrep archive

curl -LO https://github.com/BurntSushi/ripgrep/archive/0.7.1.zip
unzip 0.7.1.zip
cd ripgrep-0.7.1
ls

rg fast README.MD 
  • Note: If you see an error message from ripgrep saying that it didn’t search any files, then re-run ripgrep with the –debug flag. One likely cause of this is that you have a * rule in a $HOME/.gitignore file.

Regex

rg 'fast\w+' README.md

rg 'fast\w*' README.md

Automatic filtering

  • --no-ignore Don’t ignore .gitignore and other ignore-related filtering.

  • --hidden | -. Don’t ignore hidden files.

  • --text | -a Search binary files.

  • --follow | -L Follow symbolic links.

  • --unrestricted | -u Ignore less: -u will disable .gitignore handling, -uu will search hidden files and directories and -uuu will search binary files.

  • .ignore Files contents override .gitignore with !log/ syntax to offset .gitignore rules.


Globbing

  • -g, --glob <GLOB>... Include or exclude files.

  • rg clap -g '*.toml' Single quotes to prevent our shell from expanding the *.

  • rg clap -g '!*.toml' Won’t include files ending with .toml.


Using Type Lists

  • rg --type-list Show all supported file types.

  • rg clap -g '*.txt' Use glob syntax.

  • rg clap -t txt Or type list name.

  • rg 'fn run' --type rust Ripgrep supports certain file types.

  • rg 'fn run' -trust Or shortcut for Rust file type.

  • rg 'int main' -g '*.{c,h}' So, this is a glob.

  • rg 'int main' -tc And this is a file type list, but also the same thing.


Not in Type List

  • rg clap --type-not rust You can blacklist with file types.

  • rg clap -Trust Or simply capitalize the ‘T’ and you exclude files of this type.


User Defined Type Lists

  • Define your own types
    rg --type-add 'web:*.html' --type-add 'web:*.css' --type-add 'web:*.js' -tweb title
  • Same, define your own type.
    rg --type-add 'web:*.{html,css,js}' -tweb title
  • Must create a shell alias to persist.
    alias rg="rg --type-add 'web:*.{html,css,js}'"
  • Or add this command to a ripgrep configuration file.
    --type-add=web:*.{html,css,js}

File encoding

  • The most popular encodings likely consist of ASCII, latin1 or UTF-8. As a special exception, UTF-16 is prevalent in Windows environments

  • `--encoding auto Ripgrep’s default behavior.

  • To handle the UTF-16 case, ripgrep will perform “BOM sniffing” by default. That is, the first three bytes of a file will be read, and if they correspond to a UTF-16 BOM, then ripgrep will transcode the contents of the file from UTF-16 to UTF-8, and then execute the search on the transcoded version of the file.

  • -E/--encoding Permits you to specify an encoding

  • Will not search PDFs without preprocessing.


Common options

  • -h: Show ripgrep’s condensed help output.
  • --help: Show ripgrep’s longer form help output. (Nearly what you’d find in ripgrep’s man page, so pipe it into a pager!)
  • -i/--ignore-case: When searching for a pattern, ignore case differences. That is rg -i fast matches fast, fASt, FAST, etc.
  • -S/--smart-case: This is similar to --ignore-case, but disables itself if the pattern contains any uppercase letters. Usually this flag is put into alias or a config file.
  • -F/--fixed-strings: Disable regular expression matching and treat the pattern as a literal string.
  • -w/--word-regexp: Require that all matches of the pattern be surrounded by word boundaries. That is, given pattern, the --word-regexp flag will cause ripgrep to behave as if pattern were actually \b(?:pattern)\b.
  • -c/--count: Report a count of total matched lines.
  • --files: Print the files that ripgrep would search, but don’t actually search them.
  • -a/--text: Search binary files as if they were plain text.
  • -U/--multiline: Permit matches to span multiple lines.
  • -z/--search-zip: Search compressed files (gzip, bzip2, lzma, xz, lz4, brotli, zstd). This is disabled by default.
  • -C/--context: Show the lines surrounding a match.
  • --sort path: Force ripgrep to sort its output by file name. (This disables parallelism, so it might be slower.)
  • -L/--follow: Follow symbolic links while recursively searching.
  • -M/--max-columns: Limit the length of lines printed by ripgrep.
  • --debug: Shows ripgrep’s debug output. This is useful for understanding why a particular file might be ignored from search, or what kinds of configuration ripgrep is loading from the environment.