Ignore noisy lock files when using Telescope live_grep

Tired of seeing noisy lock files like yarn.lock or package-lock.json in your search results?

I think I found a decent solution.

Make sure you have ripgrep, a requirement for Telescope’s live_grep.

ripgrep has a --ignore-file flag which accepts a file including a list of strings to ignore.

Setup ignore-file

I’ve created a ripgrep/ignore directory/file in my dotfile directory. On my systems, they are on $HOME/dotfiles/ripgrep/ignore

$ cat ~/dotfiles/ripgrep/ignore
**/yarn.lock
**/package-lock.json
**/poetry.lock
.git/**

Create your own file, either use the ignore that I’ve made or make your own. Go ahead and test it. Here I’m searching for 123 in a project with a yarn.lock-file.

$ rg 123
yarn.lock
616:  resolved "https://registry.yarnpkg.com/@esbuild/linux-riscv64/-/linux-riscv64-0.18.20.tgz#6628389f210123d8b4743045af8caa7d4ddfc7a6"
3514:  resolved "https://registry.yarnpkg.com/tsconfig-resolver/-/tsconfig-resolver-3.0.1.tgz#c9e62e328ecfbeaae4a4f1131a92cdbed12350c4"

Now, with our ignore-file

$ rg 123 --ignore-file $HOME/dotfiles/ripgrep/ignore
$
# No results!

Integrate with telescope

live_grep has a additional_args option where you can send flags down to the rg binary. We can send our ignore-file to it.

local IGNORE_FILE = os.getenv("HOME") .. "/dotfiles/ripgrep/ignore"
require("telescope.builtin").live_grep({
  hidden = true,
  additional_args = {
    "--ignore-file=" .. IGNORE_FILE,
  },
})

I’m using legendary.nvim to setup my keybinds. Here’s my lua conf.

local IGNORE_FILE = os.getenv("HOME") .. "/dotfiles/ripgrep/ignore"
require("legendary").setup({
  keymaps = {
    {
      "<leader>gw",
        function()
          require("telescope.builtin").live_grep({
            hidden = true,
            additional_args = {
              "--ignore-file=" .. IGNORE_FILE,
            },
          })
        end,
        description = "Search in current dir/live grep",
    }
  }
})

There you go! Now your searches should be golden.