junegunn / fzf.vim

fzf :heart: vim
MIT License
9.59k stars 582 forks source link

Preview doesn't work when running Rg on specific file #1379

Closed tonebender closed 2 years ago

tonebender commented 2 years ago

If I search with :Rg "my search pattern" /path/file/to/search.txt, and results are found, the results (lines) will be listed in the left box as usual, but the preview window on the right will only say "File not found 1", where "1" is the line number where the match was found in the file (!).

If I do the same search without specifying an explicit path to Rg, like this: :Rg "my search pattern", it will find the match(es) and the preview will work as usual.

I have this custom Rg setup in my ~/.vimrc: command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".<q-args>, 1, fzf#vim#with_preview(), <bang>0)

Any ideas why the preview doesn't work with Rg calls with path/file specified?

junegunn commented 2 years ago

Because when you pass a file name to rg, it stops printing the filename. The solution is to add --with-filename.

$ rg --column --line-number --no-heading --color=always --smart-case foo | grep man1
man/man1/fzf.1:966:21:     INITIAL_QUERY="foobar"
$ rg --column --line-number --no-heading --color=always --smart-case foo man/man1/fzf.1
966:21:     INITIAL_QUERY="foobar"
$ rg --column --line-number --no-heading --color=always --smart-case --with-filename foo man/man1/fzf.1
man/man1/fzf.1:966:21:     INITIAL_QUERY="foobar"
tonebender commented 2 years ago

Great, that certainly makes sense! Thanks so much for the quick response!