Ripgrep Cheatsheet
This is my cheatsheet reference for Ripgrep ( rg
). I’ve pulled out the most interesting and useful parts I use. For a comprehensive guide, refer to the manual ( man rg
), the help ( rg -h
), and the user guide.
Cheatsheet
Some of rg
’s default behaviours to be aware of:
- Strings are treated as regular expressions
- Respects your
.gitignore
file(s) - Ignores hidden files, hidden directories, and binary files
- Searches recursively (subdirectories)
- Case sensitive
Search Options | Flags |
---|---|
Invert match (exclude string) | -v or --invert-match |
Ignore case | -i or --ignore-case |
Search case insensitively if all lowercase | -S or --smart-case |
Treat all patterns as literals (non-regex) | -F or --fixed-strings |
Ignore .gitignore, .ignore, etc. | --no-ignore |
Search hidden files/directories | -. or --hidden |
Limit number of matching lines | -m <NUM> or --max-count <NUM> |
Filter Options | Flags |
---|---|
Include specific paths (glob) | -g "src/*" will search in the src directory |
Exclude specific paths (glob) | -g !"*tests" will exclude files if tests in path |
Include/exclude paths case insensitively | --iglob |
Process all glob patterns case insensitively | --glob-case-insensitive |
Only search specific file types | -tlua , -tjson , -tc , -tcpp , -tjs , -tts , and more |
Only search custom-specified file types (glob) | -g *"*.{c, js}" |
Don’t search specific file types | -Tlua , -Tjson , -Tc , -Tcpp , -Tjs , -Tts , and more |
Output Options/Modes | Flags |
---|---|
Show NUM lines after each match | -A <NUM> |
Show NUM lines before each match | -B <NUM> |
Show NUM lines before and after each match | -C <NUM> |
Show column numbers | --column |
Hide line numbers | -N |
Count (number of occurrences) | -c |
Print files that would be searched, without performing search | --files |
Don’t group matches by each file | --no-heading |
Trim prefix whitespace from matches | --trim |
Only show filenames that don’t contain matches | --files-without-match |
Other Stuff | Flags |
---|---|
Search through compressed files (e.g. .bz2 ) | -z |
Show all supported file types and their corresponding globs | --type-list |
Example default output of rg
:
1
2
3
4
5
6
7
8
9
10
11
nzkj@nzkj projects % rg "unordered"
algorithms/arrays-and-hashing/group-anagrams.cpp
19: std::unordered_map<std::string, std::vector<int>> string_indexes;
algorithms/arrays-and-hashing/contains-duplicate.cpp
1:#include <unordered_set>
20: std::unordered_set<int> unique_values;
algorithms/arrays-and-hashing/two-sum.cpp
1:#include <unordered_map>
17: std::unordered_map<int, int> num_index;
Where else can I use Ripgrep?
I primarily use Ripgrep through Neovim via the Telescope plugin and the Telescope Live Grep Args add-on. This allows me to write a rg
prompt and have the results live update with each keystroke, along with a file preview of the result. I appreciate the consistent experience between my terminal and editor that the add-on provides. I also value the ability to perform highly specific searches to help me narrow down on what I’m looking for.
Other side notes
I frequently search through .bz2
compressed log files. I used to use bzgrep
until I realised rg
has the -z
flag, which allows searching through many different kinds of compressed files. If your compressed files are on a server without rg
(and you can’t install it), consider using sshfs
to mount the remote directory to your local machine, and then use rg -z
to search through the files. You can use sshfs
to mount a directory using the following command:
1
2
mkdir /path/to/mountpoint
sshfs user@remotehost:/path/to/directory /path/to/mountpoint
- For directories relative to home use
user@remotehost:~/path/to/directory
, e.g.nzkj@myserver:~/projects/my_project
- For directories relative to root use
user@remotehost://path/to/directory
, e.g.nzkj@myserver://tmp/
(note the double slash for/tmp/
)
You can unmount using the following command:
1
sudo umount /path/to/mountpoint
I found that rg
was significantly faster than bzgrep
. In fact, searching through a year’s worth of logs, rg
took 5.5s and bzgrep
1m5s!