Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live grep example #15

Closed
hesseltuinhof opened this issue Nov 30, 2021 · 3 comments
Closed

Live grep example #15

hesseltuinhof opened this issue Nov 30, 2021 · 3 comments

Comments

@hesseltuinhof
Copy link
Contributor

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 ❤️

@mfussenegger
Copy link
Owner

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
Copy link
Contributor Author

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants