diff --git a/README.md b/README.md index 994d7a938..5cbcd8170 100644 --- a/README.md +++ b/README.md @@ -147,9 +147,10 @@ https://github.com/user-attachments/assets/5d0e1ce9-642c-4c44-aa88-01b05bb86abb function() require('fff').live_grep({ grep = { modes = { 'fuzzy', 'plain' } } }) end, desc = 'Live fffuzy grep', }, - { "fc", - function() require('fff').live_grep({ query = vim.fn.expand("") }) end, - desc = 'Search current word', + { "fw", + function() require('fff').live_grep_under_cursor() end, + mode = { 'n', 'x' }, + desc = 'Search current word / selection', }, }, } @@ -183,6 +184,7 @@ vim.keymap.set('n', 'ff', function() require('fff').find_files() end, { desc = ' ```lua require('fff').find_files() -- find files in current repo require('fff').live_grep() -- live content grep +require('fff').live_grep_under_cursor() -- grep in normal, selection in visual require('fff').scan_files() -- force rescan require('fff').refresh_git_status() -- refresh git status require('fff').find_files_in_dir(path) -- find in a specific dir diff --git a/lua/fff/main.lua b/lua/fff/main.lua index 1418a2d71..1bc1ca832 100644 --- a/lua/fff/main.lua +++ b/lua/fff/main.lua @@ -56,6 +56,27 @@ function M.live_grep(opts) picker_ui.open(picker_opts) end +--- Live grep prefilled with the current word (normal mode) or the visual selection (visual mode). +--- @param opts? table Forwarded to `live_grep`; `query` is overwritten by the resolved text. +function M.live_grep_under_cursor(opts) + local mode = vim.fn.mode() + local query + if mode == 'v' or mode == 'V' or mode == '\22' then + -- Exit visual so '< / '> marks settle, then read the range directly — + -- no yank, no register clobber. + vim.cmd('normal! ' .. vim.api.nvim_replace_termcodes('', true, false, true)) + local s = vim.fn.getpos("'<") + local e = vim.fn.getpos("'>") + local lines = vim.fn.getregion(s, e, { type = mode }) + query = table.concat(lines, ' ') + else + query = vim.fn.expand('') + end + + opts = vim.tbl_deep_extend('force', opts or {}, { query = query }) + M.live_grep(opts) +end + --- Changes the directory indexed by the file picker to the git root and opens the file picker --- @deprecated Use `find_files` instead function M.find_in_git_root()