mfussenegger / nvim-fzy

Fuzzy picker & vim.ui.select implementation via fzy for neovim
GNU General Public License v3.0
54 stars 4 forks source link

Live grep example #15

Closed hesseltuinhof closed 2 years ago

hesseltuinhof commented 2 years ago

Is there an easy way to configure a keybinding that allows for live grep across the working directory?

I am currently stuck with fzy.execute('ag --nobreak --noheading .', fzy.sinks.edit_file)<cr>". This gives me something like this

image

Of course, sinks.edit_file can't open the selection and we need to further parse it. How best to proceed here? Any pointers are appreciated :heart:

mfussenegger commented 2 years ago

You could create a custom sink. Maybe something like this:

local M = {}

function M.edit_ag_selection(selection)
  if not selection then
    return
  end
  local parts = vim.split(selection, ':')
  local path = parts[1]
  vim.cmd('e ' .. path)
end

You'd put it somewhere in your ~/.config/nvim/lua/, for example in ~/.config/nvim/lua/myfzy.lua and then use

fzy.execute('ag --nobreak --noheading .', require('myfzy').edit_ag_selection)<cr>
hesseltuinhof commented 2 years ago

Works like a charm.

I just replaced

local path = parts[1]
vim.cmd('e ' .. path)

with

local path, line = parts[1], parts[2]
vim.cmd("e +" .. line .. " " .. path)

Is such a live grep sink something you would consider adding? I can additionally test with rg and prepare a quick PR.

mfussenegger commented 2 years ago

Adding just the sink should be fine if the format works for other tools as well.