fzf.vim icon indicating copy to clipboard operation
fzf.vim copied to clipboard

Sink in quickfix with filename & linenum from fzf#vim#grep

Open morphheus opened this issue 7 years ago • 3 comments

  • Category
    • [x] Question
    • [ ] Bug
    • [ ] Suggestion
  • OS
    • [x] Linux
    • [ ] macOS
    • [ ] Windows
    • [ ] Etc.
  • Vim
    • [ ] Vim
    • [x] Neovim

I'd like to populate the quickfix list with line number from the output of a fzf#vim#grep command.

I'm using the command:

command! -bang FLines call fzf#vim#grep(
    \'grep -vnITr --color=always "^$"',
    \ 0, 
    \ {'options': '--reverse --prompt "FLines> "'})

to recursively grep all lines within the pwd. Therefore, I need to populate the quickfix list with both filepath and line number to accurately jump to the search results.

I tried to implement something similar in this comment, but I failed to get anywhere. For some reason, I can't do any introspection while inside build_quickfix_list: I can't echo or assign to a global variable to try to figure out how to break up a:lines in setqflist:

function! s:build_quickfix_list(lines)
  echom a:lines                        " Does not work?
  let g:tml_variable = cope(a:lines)   " Does not work?
  " TODO: split a:lines between filename, linenum and text
  call setqflist(map(copy(a:lines), '{ "filename": v:val, "lnum": <?>, "text": <?> }'))
  copen
  cc
endfunction
  1. Why can't I echo / set global vars from this function?
  2. Am I on the right track to set the quickfix list with filepath/linenum?

morphheus avatar Apr 17 '18 15:04 morphheus

I want this exactly! Has anyone figured out how to do this?

arowe92 avatar Jul 09 '21 18:07 arowe92

I hope this helps: this action calls this function to populate the quickfix list with selected items from fzf results.

This is written in lua, however it can be easily translated to VimL.

arsham avatar Sep 02 '21 20:09 arsham

Why can't I echo / set global vars from this function?

By "can't" you mean what? Most likely s:build_quickfix_list doesn't get called. And if you pass a command, it receives only the first filename. But actually what you want works out of the box. You do :Rg [PATTERN], optionally filter the results, press Alt-A to select all the lines, then Enter, and a quickfix list with the results appears. It doesn't appear if you select only one line.

x-yuri avatar Dec 11 '21 14:12 x-yuri