Efficient File Search and Preview with RipGrep, FZF, and Bat
As a developer, you often need to search for code snippets, files, or function definitions. Especially when working with larger projects…
As a developer, you often need to search for code snippets, files, or function definitions. Especially when working with larger projects, this can quickly become time-consuming and cumbersome. Here is where three powerful CLI tools come into play to help you work more efficiently: RipGrep (rg), FZF, and Bat. In this blog post, I’ll show you how to use these tools separately and in combination to create an optimal workflow for searching and previewing files.
Introduction to RipGrep, FZF, and Bat
What is RipGrep?
RipGrep (often shortened to rg
) is a superfast command-line search tool specifically designed for searching through source code. It combines the best features of other search tools like grep
, ag
(The Silver Searcher), and ack
. Due to its speed, flexibility, and simplicity, it has become the go-to tool for many developers.
What is FZF?
FZF is a general-purpose command-line fuzzy finder. It allows you to interactively search for text within a list of results and filter them. It’s highly flexible and can be integrated into almost any workflow to quickly search for files, directories, commands, or other strings.
What is Bat?
Bat is a cat
clone with syntax highlighting and other extra features. It not only displays file content with colorful syntax but also acts as a pager, making it an excellent tool for file preview.
1. Using RipGrep for Fast Code Searches
The basic usage of rg
is straightforward. Here are some examples:
Search for a String within a Directory
rg "function_name"
This will recursively search the current directory (or the specified directory) for the string "function_name"
.
Search Within Specific File Types
For instance, if you want to search only within .js
files:
rg "function_name" --type js
This is helpful if you know that the code you’re looking for exists only in certain file types.
You can list all supported types with:
rg --type-list
Searching in Filenames Only
Sometimes, you want to search for filenames rather than content. You can use --files
in combination with rg
:
rg --files | rg "filename"
This will list all files and filter them based on the desired filename.
2. Combining RipGrep and FZF for Interactive Search
A highly efficient combination is to use rg
together with fzf
to interactively search and filter results.
Interactive Search with RipGrep and FZF
Let’s say you want to search for a code snippet and filter the results interactively. You can combine rg
and fzf
like this:
rg "function_name" | fzf
This searches for "function_name"
and pipes the results to fzf
, allowing you to browse through them interactively. Once you select a result, fzf
returns the selected line.
Search Only Filenames with RipGrep and FZF
Sometimes, you may want to search only for filenames interactively. Use rg
with the --files
flag:
rg --files | fzf
This will list all files recursively, which you can then filter with fzf
. For example, if you're searching for .js
files, you can quickly narrow down the list by typing .js
.
3. Previewing Files with Bat
Once you’ve found the file you’re looking for with fzf
, you might want to preview its content before opening it. This is where bat
comes into play.
Integrating Bat with FZF
fzf
provides the ability to integrate a preview function. Combine fzf
with bat
to preview the selected file:
rg --files | fzf --preview 'bat --style=numbers --color=always --line-range :500 {}'
Here, fzf
will list all files, and as you navigate with the arrow keys, bat
will show a preview of the file. The --line-range :500
argument specifies that only the first 500 lines of the file should be displayed, which is usually sufficient.
Quickly Editing After Searching
After finding and previewing a file, you might want to open it directly in your preferred editor. You can configure fzf
to open the file upon selection:
rg --files | fzf --preview 'bat --style=numbers --color=always --line-range :500 {}' --bind 'enter:execute(vim {})'
With this configuration, you can navigate through files, see a preview, and open the selected file in vim
(or your preferred editor) by pressing Enter
.
Tip: Use fzf
together with aliases in your shell configuration (e.g., .bashrc
or .zshrc
) to run commands even more quickly and comfortably. An example alias might look like this:
alias search="rg --files | fzf --preview 'bat --style=numbers --color=always --line-range :500 {}' --bind 'enter:execute(vim {})'"
With this alias, you can simply type search
in the terminal to instantly start an interactive file search with preview and edit functionality.
4. Advanced Integration: RipGrep, FZF, and Bat for Interactive File Search
RipGrep Integration Walkthrough
The two core components of fzf
are its interactive terminal interface and fuzzy matching algorithm. While fzf
’s fuzzy matching is useful, it can produce irrelevant results in non-interactive environments, and user confirmation is often required. This is where rg
comes in to provide precise, non-interactive pattern matching.
By combining rg
with fzf
, you can leverage rg
's search power and fzf
's interactivity.
Step-by-Step Walkthrough
- Disabling fzf Filtering
Usefzf
's--disabled
option to prevent it from filtering, turning it into a simple selector.fzf --disabled
fzf --disabled
- Binding the Change Event
To relaunchrg
when the search string changes, usefzf
'schange:reload
binding.
fzf --disabled --bind 'change:reload:rg {q}'
- Setting RipGrep Options
Add usefulrg
flags to enhance search output:
fzf --disabled --ansi \
--bind 'change:reload:rg --column --color=always --smart-case {q}'
- Fixing the Initial List
Feedfzf
an initial list of search results:
rg --column --color=always --smart-case '' |
fzf --disabled --ansi \
--bind 'change:reload:rg --column --color=always --smart-case {q}'
- Adding a Preview Window with Bat
Integratebat
for syntax-highlighted previews:
(RELOAD='reload:rg --column --color=always --smart-case {q} || :'
fzf --disabled --ansi \
--bind "start:$RELOAD" --bind "change:$RELOAD" \
--delimiter : \
--preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \
--preview-window '+{2}/2')
- Executing Actions on Selection
Bindenter
to open selected files in your editor:
(RELOAD='reload:rg --column --color=always --smart-case {q} || :'
fzf --disabled --ansi \
--bind "start:$RELOAD" --bind "change:$RELOAD" \
--bind 'enter:become:vim {1} +{2}' \
--delimiter : \
--preview 'bat --style=full --color=always --highlight-line {2} {1}' \
--preview-window '~4,+{2}+4/3,<80(up)')
Summary
With RipGrep
, FZF
, and Bat
, you have a powerful trio at hand to improve your efficiency as a developer:
- RipGrep is excellent for blazing-fast code searches.
- FZF allows you to interactively search and filter through results, whether they’re code snippets or filenames.
- Bat enables you to elegantly preview files with syntax highlighting, making CLI work more pleasant.
By combining these tools, you can quickly search for code, find files, and edit them directly from the command line. Your workflow will become not only faster but also more enjoyable.
Try out these tools, experiment with the different options, and find the workflow that suits you best!