[New] Tools for Success - Ripgrep (rg)

Written by Tom Houlton-Harrop

Topic

Ripgrep (rg) is an incredibly useful, cross-platform, command line search tool that is remarkably helpful when hunting down log output, legacy code or misplaced notes. Anyone familiar with grep will be right at home only now Windows/cmd.exe users need not feel left out in the cold.

Case Study

A concrete example from recent memory where rg saved some time was finding out where a particular file belonged in the Lumberyard project. In this case where RenderViewport.cpp belonged in the CMake build. I opened up a terminal and navigated to the dev/ folder and ran this command:

rg RenderViewport.cpp -t cmake

This searches all CMake files (CMakeLists.txt file and .cmake files) for the regex pattern RenderViewport.cpp (-F will treat the string as a string literal if you prefer). This command returned:

Code\Sandbox\Editor\editor_lib_files.cmake
1067: RenderViewport.cpp

With that information we can then search for editor_lib_files.cmake

rg editor_lib_files.cmake -t cmake

And find exactly where it’s used:

Code\Sandbox\Editor\CMakeLists.txt # the file I'm after
90: editor_lib_files.cmake

There are of course other ways to get from A to B but this tool was incredibly useful while doing a waf → CMake conversion when hunting for file references and compiler flags was easier said than done.

Examples

rg something # search all files for the phrase 'something'
rg -F "void print()" # -F treats the pattern as a literal string (default is a regex pattern)
rg cmake_minimum_required -t cmake # Only look in cmake files (includes CMakeLists.txt and .cmake files (-t cpp works too)
rg fizzbuzz --iglob *.h # search only in files with the extension .h (useful when the file type is not supported with -t (like wscript :))
rg --files --iglob wscript # search for all files called wscript (though there's a better tool for this we'll soon see...)
rg -h # succinct help, loads more like -i (ignore case) and -C <NUM> (Line count to include above/below each match)
rg --help > rg-help-out.txt # output full help to a file (much easier for searching for what you're after with Ctrl-F)
rg an_image.png -g !Cache/ # exclude a folder from the search (the '!'). Useful for excluding things like the Cache or build folder when searching from dev/

Resources

Author: Tom Hulton-Harrop

Disclaimer: The views expressed here are those of the individual author and do not represent those of Lumberyard or Amazon.