Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to exclude file name from fzf filtering after ripgrep search results?

Tags:

vim

fzf

vim-fzf

I use a convenience package fzf.vim to integrate fzf with vim. The file search works perfect and has completely replaced CtrlP plugin, but the functionality to grep files has unfortunate side-effect of fzf thinking the auto-appended filename (by ripgrep, but this would be the case with ag (silver-searcher) and regular grep as well) is part of the search string.

As a result, the best result doesn't always come to the top (or bottom in my case based on layout). Here is an example: enter image description here The result that appears last in this case should actually be first because it's the only result that matches by content (as intended by Rg) rather than file name. This problem is especially frustrating on large repos, where irrelevant results could litter your entire search space.

I should also clarify that the problem occurs because I invoke :Rg from a shortcut, so the filtering is done by fzf, not rg (which just dumps everything to fzf).

This problem occurs in default :Rg implementation bundled with fzf.vim as well as my attempt to roll my own:

command! -bang -nargs=* Rg
  \ call fzf#vim#grep(
  \   'rg --column --line-number --no-heading --color=always --smart-case '.shellescape(<q-args>), 1,
  \   <bang>0 ? fzf#vim#with_preview('up:60%')
  \           : fzf#vim#with_preview('right:50%:hidden', '?'),
  \   <bang>0)

I couldn't figure out how to remedy this issue. Can someone help?

like image 605
Alexander Tsepkov Avatar asked Jan 23 '20 18:01

Alexander Tsepkov


2 Answers

Something that worked out for me is redefining Rg as follows:

command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1, {'options': '--delimiter : --nth 4..'}, <bang>0) 

I wish I came up with this myself, but honestly, after facing the same issue as you have, I was lucky to get this blog post in my search results. If you are like me and get confused how and why this works, this issue thread in fzf github repo describes the anatomy of the command above (and offers the same solution to the problem).

like image 168
ambushed Avatar answered Sep 21 '22 23:09

ambushed


It sounds like you want your own ripgrep shortcut as called inside vim to not return the filename when searching, so that you can use your search results down the line.

man rg:

   -I, --no-filename
       Never print the file path with the matched lines. This is the default
       when ripgrep is explicitly instructed to search one file or stdin.
       This flag overrides --with-filename.
like image 27
jeremysprofile Avatar answered Sep 20 '22 23:09

jeremysprofile