Skip to content

Recursively search a directory tree with ripgrep and fzf

License

Notifications You must be signed in to change notification settings

suvayu/fzf_search

Repository files navigation

Ripgrep + FZF

$ fzf-search [PATH...]

Search a source tree recursively with rg and fzf.

When you are working in a large repository/working directory, searching for blocks of text in hundreds of files can be difficult. This FZF interface for Ripgrep addresses that by letting you narrow down your search results recursively. A search is restricted to the set of files with matches in the previous step. At any point, you can easily switch between searching file contents or filtering file paths. Here is a mini screencast demonstrating some of these features.

screen cast of using fzf-search

PS: You can access a cheat-sheet by pressing F1 at any point.

Keybindings

  • F1 help message
  • C-s search source tree recursively
  • C-f limit files to search
  • RET - open in a pager (bat or less)
  • M-RET - open in $EDITOR

Example commands

  • Find files to search in current directory/path(s):
    $ fzf-file [PATH...]
  • Search the current directory/in paths:
    $ fzf-search [PATH...]

More documentation on usage can be found in the manual.

Dependencies

  • Ripgrep or rg for search
  • fzf for display & filtering UI
  • (optional) bat for preview. It's a cat clone with syntax highlighting support. If it isn't found in $PATH, less is used.
  • man to see the help message

Installation

You need to install the above dependencies using your platform's package manager. After that you have to make sure the scripts in this repo are in your $PATH. There are several ways you could achieve this:

  1. Checkout this repo, and add it to your $PATH

    git checkout https://github.com/suvayu/fzf_search.git
    export PATH="$PWD/fzf_search:$PATH"

    To make this permanent, set this value of $PATH in your shell's profile/rc file. For Bash that would be ~/.bash_profile or ~/.bashrc.

  2. Checkout this repo, and create symlinks to the scripts in a directory that is present in your $PATH. Say ~/bin is in your $PATH.

    git checkout https://github.com/suvayu/fzf_search.git
    cd ~/bin
    ln -s $OLDPWD/fzf_search/fzf-{search,file} .
  3. Copy over the scripts (fzf-{search,file}) and help.1 to a directory in your $PATH. Note that when copying the files you also need to copy the help file to be able to see the help message. This is because scripts try to find the help file in the same directory.

Out of the above methods, (2) is preferred if you already have a setup where you have a user directory in your $PATH, however if that's not the case, (1) is a somewhat simpler alternative. While (3) works, it is discouraged as it's difficult to update the scripts.

Running on NixOS

If you are on NixOS (or use the nix package manager independently), the repo includes a flake recipe so that you can run the scripts with:

nix run github:suvayu/fzf_search#fzf-search

or

nix run github:suvayu/fzf_search#fzf-file

Updating

If you have followed options (1) or (2) to install the scripts, you may update by navigating to the git repository, and running git pull. If you opted for option (3) to install, you need to reinstall.

FAQ

1. I see characters like ^[[0m, ^[[3m in fzf, and file preview breaks

For some reason ANSI escape codes are not being interpreted by fzf correctly, that leads to incorrect file names, which breaks the file preview. You can disable colour as a workaround.

  $ NOCOLOR=1 fzf-search [PATH...]

2. Binary files are matching

If binary files are showing up in matches, you can ignore them as:

   $ NOBINARY=1 fzf-search [PATH...]