Skip to content
Myr

Update ADVANCED.md: transform-query to restore the query string

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Close #2961

authored by Junegunn Choi · committed by Junegunn Choi · parent f38a7f7 · Browse files · 1 file+19 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+19 added−8 removed

  • other:+19 added−8 removed

1 file+19 added−8 removed

modified:ADVANCED.md+19 added-8 removed
1Advanced fzf examples2======================34*(Last update: 2022/08/25)*4*(Last update: 2023/02/12)*56<!-- vim-markdown-toc GFM -->78* [Introduction](#introduction)
398 unmodified lines
407 displayed in the second phase408409### Switching between Ripgrep mode and fzf mode410411*(Requires fzf 0.30.0 or above)*411*(Requires fzf 0.36.0 or above)*412413fzf 0.30.0 added `rebind` action so we can "rebind" the bindings that were414previously "unbound" via `unbind`.413[fzf 0.30.0][0.30.0] added `rebind` action so we can "rebind" the bindings414that were previously "unbound" via `unbind`.415416This is an improved version of the previous example that allows us to switch417between Ripgrep launcher mode and fzf-only filtering mode via CTRL-R and418CTRL-F.
1 unmodified line
420```sh421#!/usr/bin/env bash422423# Switch between Ripgrep launcher mode (CTRL-R) and fzf filtering mode (CTRL-F)424rm -f /tmp/rg-fzf-{r,f}425RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "426INITIAL_QUERY="${*:-}"427IFS=: read -ra selected < <(428 FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \429 fzf --ansi \430 --color "hl:-1:underline,hl+:-1:underline:reverse" \431 --disabled --query "$INITIAL_QUERY" \432 --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \432 --bind "ctrl-f:unbind(change,ctrl-f)+change-prompt(2. fzf> )+enable-search+clear-query+rebind(ctrl-r)" \433 --bind "ctrl-r:unbind(ctrl-r)+change-prompt(1. ripgrep> )+disable-search+reload($RG_PREFIX {q} || true)+rebind(change,ctrl-f)" \434 --prompt '1. Ripgrep> ' \433 --bind "ctrl-f:unbind(change,ctrl-f)+change-prompt(2. fzf> )+enable-search+rebind(ctrl-r)+transform-query(echo {q} > /tmp/rg-fzf-r; cat /tmp/rg-fzf-f)" \434 --bind "ctrl-r:unbind(ctrl-r)+change-prompt(1. ripgrep> )+disable-search+reload($RG_PREFIX {q} || true)+rebind(change,ctrl-f)+transform-query(echo {q} > /tmp/rg-fzf-f; cat /tmp/rg-fzf-r)" \435 --bind "start:unbind(ctrl-r)" \436 --prompt '1. ripgrep> ' \437 --delimiter : \436 --header '╱ CTRL-R (Ripgrep mode) ╱ CTRL-F (fzf mode) ╱' \438 --header '╱ CTRL-R (ripgrep mode) ╱ CTRL-F (fzf mode) ╱' \439 --preview 'bat --color=always {1} --highlight-line {2}' \440 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3'441)442[ -n "${selected[0]}" ] && vim "${selected[0]}" "+${selected[1]}"443```444445- To restore the query string when switching between modes, we store the446 current query in `/tmp/rg-fzf-{r,f}` files and restore the query using447 `transform-query` action which was added in [fzf 0.36.0][0.36.0].448- Also note that we unbind `ctrl-r` binding on `start` event which is449 triggered once when fzf starts.450451[0.30.0]: https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0300452[0.36.0]: https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0360453454Log tailing455-----------456457fzf can run long-running preview commands and render partial results before
163 unmodified lines