Simplify Ripgrep Search with Advanced Fzf Binding

Imagine spending most of your time in the terminal searching for files using classic commands like find, grep, and cat. While these tools are efficient, you might find them awkward to use. This article introduces a more advanced approach using fzf and ripgrep, which delivers a powerful search experience.

To start, we’ll explore how fzf’s “change” event can be used to execute a command when the query changes. We’ll build on this by configuring ripgrep with flags that enhance its search capabilities. The script uses these flags to pipe the results into fzf, making it easier to navigate and open files.

The “–bind” flag associates events with external commands in fzf. For example, “change:reload:sleep 0.1;” executes a command when the query changes. This allows us to reload ripgrep with new search settings every time we type something.

We’ll use the following flags to configure ripgrep:

– “–column”: includes a column number in the results.
– “–no-heading”: removes unnecessary headers from the output.
– “–color=always”: displays colored text for better readability.
– “–smart-case”: filters results by case only when the query contains uppercase letters.

These quality-of-life features significantly enhance the search experience. We’ll also use fzf’s “preview” flag to display rich results in a preview window, making it easier to choose a result.

To open files at a specific location with any text editor (Nano, Vim, or Neovim), we’ll bind a command that opens the file at the specified line and column. For Emacs, we’ll use a different binding.

The script is then placed in the bashrc configuration file and executed by typing “s.” You can pass ripgrep flags to it, including a path.

While this approach provides an advanced search experience, there’s one major flaw: indexing your entire file system contents would be huge. To speed up searches, consider passing ripgrep a path to narrow the scope or adding additional filtering flags.

Note: To use this script, simply copy and paste it into your bashrc configuration file and reload it. Then, type “s” in the terminal to start searching with advanced fzf binding.

Source: https://www.howtogeek.com/how-one-bash-function-gives-me-real-time-search-across-thousands-of-files