u/BigTanuki64

▲ 1 r/neovim

[Neovim tutorial] How to exclude directories from fzf-lua

I found an old post here on r/neovim asking about this exact thing, but it was archived so I couldn't reply with my solution, and none of the comments there helped me. Anyway, here's how I did it:

local fzf_lua = require('fzf-lua')

-- get the default options, later we modify/add to them
local rg_defaults = fzf_lua.config.defaults.grep.rg_opts
local fd_defaults = fzf_lua.config.defaults.files.fd_opts

vim.keymap.set('n', '<Leader>p', function()
  fzf_lua.files({
    fd_opts = fd_defaults
      .. " --exclude third-party" -- exclude dirs from the `files` command like this; note the extra leading space
      .. " --exclude deps"
  }) end,
  { desc = 'Fzf Files'})

vim.keymap.set('n', '<Leader>f', function()
  fzf_lua.live_grep({
    rg_opts = rg_defaults:gsub(" %-e$", "") -- remove the final `-e`; `%-` means 'literal dash' in lua
      .. " -g '!third-party/*'" -- exclude dirs from the `live_grep` command like this
      .. " -g '!deps/*'"
      .. " -e" -- add the final `-e` back; this is required because `rg` expects everything after `-e` to be the search pattern
  }) end,
  { desc = 'Fzf Grep'})
reddit.com
u/BigTanuki64 — 10 days ago