From 4c89fa6339f6764f41a1b4e067ca3f6e85e0a45e Mon Sep 17 00:00:00 2001 From: t0xa Date: Sat, 17 Oct 2026 15:38:05 +0300 Subject: [PATCH] FzfLua: Add command to search in filtered files --- lua/custom/fzf_lua.lua | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lua/custom/fzf_lua.lua b/lua/custom/fzf_lua.lua index 73c8166..2e82df9 100644 --- a/lua/custom/fzf_lua.lua +++ b/lua/custom/fzf_lua.lua @@ -87,13 +87,51 @@ local function live_grep_in_quick_fix_list_files() if #files > 0 then fzf_lua.live_grep({ - search_dirs = files, + search_paths = files, }) else print("No files in quickfix list") end end +-- Two-step search in cwd: +-- 1) type a literal file name prefix (e.g. `Go - `, case-insensitive), multi-select +-- or to take all matches +-- 2) live_grep restricted to the files picked in step 1 +local function grep_in_filtered_files() + local cwd = vim.fn.getcwd() + + -- rg matches the glob against the file name only; escape glob metacharacters + local function files_cmd(args) + local prefix = (args[1] or ""):gsub("[%*%?%[%]{}\\]", "\\%0") + return string.format("rg --files --color=never --iglob %s", vim.fn.shellescape(prefix .. "*")) + end + + ---@diagnostic disable-next-line: missing-fields + fzf_lua.fzf_live(files_cmd, { + cwd = cwd, + prompt = "Filename starts with❯ ", + exec_empty_query = true, + fzf_opts = { ["--multi"] = true }, + actions = { + ["default"] = function(selected) + if not selected or #selected == 0 then + return + end + local files = {} + for _, name in ipairs(selected) do + table.insert(files, cwd .. "/" .. name) + end + fzf_lua.live_grep({ + cwd = cwd, + prompt = string.format("Grep in %d files❯ ", #files), + search_paths = files, + }) + end, + }, + }) +end + local function find_all_files_including_hidden() fzf_lua.files( { @@ -125,6 +163,7 @@ keymap("n", "fh", function() fzf_lua.help_tags() end, { desc = "FZF: dis keymap("n", "fa", find_all_files_including_hidden, { desc = "FZF: Find all files" }) keymap('n', 'fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' }) keymap('n', 'fcp', live_grep_in_current_dir, { desc = 'Live grep in current directory' }) +keymap('n', 'fv', grep_in_filtered_files, { desc = "Filter files by name, then grep inside them" }) keymap('n', 'ccp', copy_file_path_and_position, { desc = "Copy cursor position" }) keymap('n', 'ccd', copy_current_file_path, { desc = 'Copy current file path' }) keymap('n', 'ccf', copy_filename, { desc = 'Copy current file path' })