how to make ripgrep fzf integration command searching both file name and content
- [X] I have read through the manual page (
man fzf) - [X] I have the latest version of fzf
- [X] I have searched through the existing issues
Info
- OS
- [X] Linux
- [ ] Mac OS X
- [ ] Windows
- [ ] Etc.
- Shell
- [ ] bash
- [ ] zsh
- [X] fish
I found the integration commands of ripgrep and fzf is very handy, but one thing missing for me: it could NOT search patten in file name
The codes are simpliy copied from advanced.md
#!/usr/bin/env bash
# 1. Search for text in files using Ripgrep
# 2. Interactively narrow down the list using fzf
# 3. Open the file in Vim
IFS=: read -ra selected < <(
rg --color=always --line-number --no-heading --smart-case "${*:-}" |
fzf --ansi \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--delimiter : \
--preview 'bat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)
[ -n "${selected[0]}" ] && vim "${selected[0]}" "+${selected[1]}"
I saw several posts about the opposite sides, that only matches in file content are desired with fzf.vim.
How could I modify the bash command above to search the patten in file name and file content? Thanks!
did something similar to search patterns in both file name and file content at the same time with fzf+rg+neovim (mine was for searching a dir containing various cheatsheets/manpages, so I can search matches by both name and/or content in fzf)
Fish Shell (can be converted to bash/zsh easily):
function fzfrg
# rg -n -H . ~/Documents --color=always| fzf --preview "tail -n +(echo {} | cut -d':' -f2) < (echo {} | cut -d':' -f1)" --color
rg -n -H . --color=always| fzf --preview "tail -n +(echo {} | cut -d':' -f2) < (echo {} | cut -d':' -f1)" --color
end
function fzfrgvim
# nvim -c :Fzfrgvim
nvim -c :Fzfrgvim (pwd)
# :BLines
# :Line
end
init.vim:
command Fzfrgvim :Rg!

@ccshao
Thank your very much for the solutions from different angles, nvim -c :Fzfrgvim (pwd) looks straightforward.
When runnning fzfrg in fish, it searches in the current folder. However the preview didnt show correctly. It didn't parse the file path correctly.

I couldn't get @DevAtDawn's preview to work either ...
This is what I have (this recipe uses rg, bat and fzf)

# rg
# --field-match-separator ' ' - tell rg to separate the filename and linenumber with
# spaces to play well with fzf, (when recognizing index variables to use in the fzf
# preview command, fzf uses a default delimiter of space, see below)
# fzf
# --preview window ~8,+{1}-5
# this is a fzf feature
# ~8 - show first 8 lines (header)
# +{2} - fzf delimits the input piped in to it and provides access via index variables {n}.
# the default delimiter fzf uses is space but can be specified via --delimiter <delimiter>
# pass the second index variable from bat (which is the line number)
# the number is signed, you can show eg the +n row or the -n row (the nth row from the bottom)
# -5 subtract 5 rows (go up 5 rows) so that you don't show the highlighted line as the first line
# since you want to provide context by showing the rows above the highlighted line
rg --line-number --with-filename . --color=always --field-match-separator ' '\
| fzf --preview "bat --color=always {1} --highlight-line {2}" \
--preview-window ~8,+{2}-5
rg --color=always --line-number --no-heading --smart-case "${*:-}" ...
I had an issue with the parameters being passed to ripgrep as "${*:-}". Using "${@:-}" or simply "$@" allowed me to pass a search string and a pattern such as -g '*.sh'
The options above didn't work for me so I created one that works for me
#!/bin/sh
_file="$(rg --color=always --line-number --no-heading --smart-case "${@:-^[^\n]}" \
| fzf -d ':' --preview 'bat --style=numbers --color=always $(cut -d: -f1 <<< {1}) --highlight-line {2} --line-range={2}:+20' \
--preview-window='50%' --height='50%' --with-nth 1,3.. --exact)"
_file="${_file%%:*}"
[ -z "$_file" ] && exit
exec nvim "$_file"
Checkout https://news.ycombinator.com/item?id=38471822