Fzf can (now!) be used as a ripgrep searcher AND filter ❤️❤️
bew opened this issue · comments
Benoit de Chezelles commented
Latest fzf (0.27.1) allows to change options at runtime,
👉 See https://github.com/junegunn/fzf/blob/master/ADVANCED.md#switching-to-fzf-only-search-mode
NOTE: in the example, it goes from ripgrep.... then filtering... BUT it's not described how to go back to ripgrep.
👉 Need to try to make it work!!!!!!!
Dump of the example here
Switching to fzf-only search mode
(Requires fzf 0.27.1 or above)
In the previous example, we lost fuzzy matching capability as we completely
delegated search functionality to Ripgrep. But we can dynamically switch to
fzf-only search mode by "unbinding" reload
action from change
event.
#!/usr/bin/env bash
# Two-phase filtering with Ripgrep and fzf
#
# 1. Search for text in files using Ripgrep
# 2. Interactively restart Ripgrep with reload action
# * Press alt-enter to switch to fzf-only filtering
# 3. Open the file in Vim
RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
IFS=: read -ra selected < <(
FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \
fzf --ansi \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--disabled --query "$INITIAL_QUERY" \
--bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
--bind "alt-enter:unbind(change,alt-enter)+change-prompt(2. fzf> )+enable-search+clear-query" \
--prompt '1. ripgrep> ' \
--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]}"
- We added
--prompt
option to show that fzf is initially running in "Ripgrep
launcher mode". - We added
alt-enter
binding that- unbinds
change
event, so Ripgrep is no longer restarted on key press - changes the prompt to
2. fzf>
- enables search functionality of fzf
- clears the current query string that was used to start Ripgrep process
- and unbinds
alt-enter
itself as this is a one-off event
- unbinds
- We reverted
--color
option for customizing how the matching chunks are
displayed in the second phase