ripgrep & fd - Command-line Search Tools
The grep and
find utilities are ubiquitous
search work-horses for Unix-like systems. The grep
utility, as many reading
this post will be aware, is used to search for patterns within files whilst
find
is primarily used to match file names based on supplied expressions.
Both of these legacy tools are still valid today many decades after their initial release. However, a new wave of tools has recently appeared that make command-line searching easier, faster and more user-friendly.
This post highlights a couple such actively-developed tools, namely ripgrep and fd.
ripgrep (pattern search)
The ripgrep utility is a line-oriented search tool that recursively searches within the current directory tree for a supplied pattern.
Written in Rust, ripgrep has excellent performance that is as fast, and more often faster, than other similar pattern search tools.
Programmer-friendliness is provided by the following niceties:
-
ripgrep is automatically recursive, unlike
grep
, thus only a search pattern need be supplied -
ripgrep will automatically ignore all directories and files specified in
.gitignore
files -
ripgrep search hits and file matches are clearly highlighted in distinctive color
-
ripgrep is invoked by the short two-letter command
rg
, two fewer letters thangrep
Installation
ripgrep is easily installed using Homebrew on macOS and Linux.
brew install ripgrep
For other platforms please consult these installation details.
Usage
rg Foo # Case sensitive search
rg -i foo # Case insensitive search
rg -v foo # Invert search, show all lines that don't match pattern
rg -l foo # List only the files that match, not content
rg -t md foo # Match by `md` file extension
Screenshot
Ignores
As noted already, ripgrep will automatically ignore content specified in
.gitignore
files.
However, when not in a Git tree, or when wanting to override .gitignore
,
ripgrep also respects, with higher precedence, ignores specified in .rgignore
files.
For instance, the next snippet will create a global ripgrep rule to ignore
content in tmp
directories.
echo tmp >> ~/.rgignore
Vim integration
If you are a Vim user, then I recommend the following
settings to integrate ripgrep as the preferred external grepprg
:
set grepprg=rg\ --vimgrep\ --smart-case
set grepformat=%f:%l:%c:%m,%f:%l:%m
fd (file find)
The fd utility is a file finding tool. It is a
modern-day simplified alternative to the Unix find
tool.
Like ripgrep, fd is also written in Rust and likewise has excellent performance. Note, I am led to believe that ripgrep and fd do share some code.
Programmer-friendliness is provided by the following niceties:
-
fd is automatically recursive, thus only a search pattern need be supplied
-
fd will automatically ignore all directories and files specified in
.gitignore
files -
fd matches are colorized
-
fd is invoked by the short two-letter command
fd
, two fewer letters thanfind
Lastly, it should be noted that fd does not provide all the functionality
of find
, however it sensibly provides functionality for the most common
use-cases.
Installation
fd is easily installed using Homebrew on macOS and Linux.
brew install fd
For other platforms please consult these installation details.
Usage
fd # List all non-ignored files
fd foo # Case insensitive search
fd Foo # Case sensitive search
fd --type f foo # Match only files
fd --type d foo # Match only directories
fd -e md foo # Match by `md` file extension
fd '^[0-9]' # List files starting with a digit
Demonstration
Ignores
As noted already, fd will automatically ignore content specified in
.gitignore
files.
However, when not in a Git tree, or when wanting to override .gitignore
,
fd also respects, with higher precedence, ignores specified in .fdignore
files.
For instance, the next snippet will create a global fd rule to ignore
content in tmp
directories.
echo tmp >> ~/.fdignore
fzf
The fzf utility is a generic command-line
fuzzy finder. If you are already using fzf and are not using fd as the file
finder than I recommended adding the following settings to your shell’s
configuration file (e.g ~/.bashrc
).
export FZF_DEFAULT_COMMAND='fd --type f --color=never'
export FZF_ALT_C_COMMAND='fd --type d . --color=never'
If you are unfamiliar with fzf, stay tuned for an upcoming post in this channel.
UPDATE (DEC 2018): Please read fuzzy finding in Bash with fzf and fuzzy finding in Vim with fzf
Conclusion
As an old-school command-line user I find both ripgrep and fd to now be indispensable tools. Give them a try yourself, maybe you will like them too.