4

When I run a project-wide (multi-file) search through FZF, the results are flooded with lines whose filepath matches the query, crowding out the more relevant results where the file contents matched.

For example, running the Ag example from the wiki,

ag --nobreak --nonumbers --noheading . | fzf

I'll query for search and get something like this,

  plugin/keybindings.vim:cnoremap <expr> <C-Y> refract#if_incsearch("\<C-L>", "\<C-Y>")
  plugin/keybindings.vim:cmap            <C-P> <Plug>(refract_incsearch_prev)
  plugin/coherent.vim:set incsearch
  plugin/coherent.vim:setg tags+=./tags;~  " search for tags recursively upwards until ~
  plugin/search.vim:" use it for tab-completion instead depending on context of cmdline
  plugin/search.vim:  autocmd User Hint,listical_next,listical_prev Latitude
  plugin/search.vim:  autocmd User Grepper call hint#prepare_highlights()
  plugin/search.vim:let g:fzf_colors =
  plugin/search.vim:" let g:grepper =
  plugin/search.vim:\   <q-args>,
  plugin/search.vim:\   <bang>0)
  plugin/search.vim:augroup END
  plugin/search.vim:  autocmd!
  plugin/search.vim:
  plugin/search.vim:
  plugin/search.vim:
  plugin/search.vim:
> plugin/search.vim:
  41/517
> search

What am I doing wrong? When I run a similar Ag query without FZF, I only get the results where my query occurred in the file contents (which is what I want).

ivan
  • 1,055

2 Answers2

4

I was misunderstanding the interaction between ag and fzf, thinking the query I typed was somehow passed through ag before piping to fzf. In retrospect, this was a silly mistake to make, since the filtering is exactly what fzf does, and ag is being used simply as a way to generate tons of meaningful lines.

To get what I wanted out of this, I looked at the format of the lines that ag generated, e.g.

plugin/coherent.vim:set incsearch

Tell fzf to treat : as the field delimiter and that it should restrict the scope of its search to fields 2, 3, 4,...

ag --nobreak --nonumbers --noheading . | fzf --delimiter=: --nth=2..

This will break if any of the processed files contain a : in their filename, but that's something I don't expect to happen too often.

ivan
  • 1,055
0

For anyone looking for a ripgrep script, this is what I came up with:

FZF_EDITOR="gvim"

Fuzzy Ripgrep Ignoring-case

frgi() { local result IFS=$'\n' result="$(rg -i --line-number "$@" | fzf --no-sort --tac --delimiter=: --nth=2..)"

local filename local lineno local _ if [[ -n "$result" ]] then IFS=':' read -r filename lineno _ <<< "$result" # Vim can accept line-number with +99 # But if your editor cannot, then you had better remove the + argument verbosely "$FZF_EDITOR" +"$lineno" "$filename" fi }

joeytwiddle
  • 1,745