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

Rg: operates also on filenames

Open venthur opened this issue 4 months ago • 5 comments

Checklist

  • [x] I have fzf 0.54.0 or later
  • [x] I have searched through the existing issues

Output of :echo system(fzf#exec() .. ' --version')

0.60 (devel)

OS

  • [x] Linux
  • [ ] macOS
  • [ ] Windows
  • [ ] Etc.

Problem / Steps to reproduce

Rg: seems to operate on filenames as well (and not just the contents of the files), in contrast to RG: which operates as expected. Is this intended? If yes, can you document it more clearly?

venthur avatar Aug 28 '25 13:08 venthur

It looks like fzf-search originally excluded the filename, but it was removed 8 years ago by this commit: d3b9fed9c2415a2682cb1c8604e25a351325c22b

It can easyly revert with this:

diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim
index 7a1c4d7..ca2f705 100755
--- a/autoload/fzf/vim.vim
+++ b/autoload/fzf/vim.vim
@@ -971,3 +971,3 @@ function! fzf#vim#grep(grep_command, ...)
   let opts = {
-  \ 'options': ['--ansi', '--prompt', capname.'> ',
+  \ 'options': ['--ansi', '--prompt', capname.'> ', '--nth', '3..',
   \             '--multi', '--bind', 'alt-a:select-all,alt-d:deselect-all',

kkvark avatar Sep 01 '25 18:09 kkvark

In Rg, fzf is used as the secondary filter. When you run :Rg foobar, fzf starts ripgrep to search for foobar, then we further narrow down the ripgrep result using fzf's fuzzy search.

In RG, on the other hand, fzf serves solely as an interactive frontend for ripgrep. The keyword you type is directly forwarded to ripgrep.

For more details, see https://github.com/junegunn/fzf/blob/master/ADVANCED.md#ripgrep-integration.

Anyway, when fzf acts as the secondary filter, I prefer enabling matches against file paths, as it allows me to refine the list based on file paths, for example:

# Search foobar
> foobar

# Limit the scope to .java or .md files
> foobar .java: | .md:

But if you do not like the behavior, you can change it like so:

let g:fzf_vim = {}
let g:fzf_vim.rg_options = '--nth 4..'

junegunn avatar Sep 02 '25 00:09 junegunn

Thanks for the explanation. I did not see the the ADVANCED doc, but was only referring to the README and the help provided in vim, which are not really helping in understanding the difference of both:

Image

Also, maybe I was using Rg: and RG: slightly wrong, because I basically use the leader key to activate RG: or Rg: respectively, and maybe for Rg: this is a rather unexpected use-case?

vim.keymap.set('n', '<leader>f', ':Files<CR>', { silent = true })
vim.keymap.set('n', '<leader>/', ':RG<CR>', { silent = true })

venthur avatar Sep 02 '25 07:09 venthur

Yeah, the documentation is sparse.

I usually avoid starting :RG or :Rg without an initial search term, since that can cause fzf to load a large number of entries into memory. Other than that, whatever floats your boat.

I personally use :Ag (mostly out of old muscle memory) and I have these mapping to quickly search for the word under the cursor, in normal mode and visual mode.

nnoremap <silent> <Leader>ag :Ag <C-R><C-W><CR>
xnoremap <silent> <Leader>ag y:Ag <C-R>"<CR>

junegunn avatar Sep 02 '25 13:09 junegunn

But if you do not like the behavior, you can change it like so: let g:fzf_vim = {} let g:fzf_vim.rg_options = '--nth 4..'

This seems to break with colons:

Image Image

I'll look into this some more when I have time, but if anyone knows right away why it's breaking it'd be much appreciated. :)

jukuisma avatar Nov 27 '25 11:11 jukuisma