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
Provenance
- Commit
719dbb8- Tree
e08be07- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
f38a7f7
1 file changed+19 added−8 removed
modified:ADVANCED.md+19 added-8 removed
1 Advanced fzf examples2 ======================3 4-*(Last update: 2022/08/25)*4+*(Last update: 2023/02/12)*5 6 <!-- vim-markdown-toc GFM -->7 8 * [Introduction](#introduction)398 unmodified lines407 displayed in the second phase408 409 ### Switching between Ripgrep mode and fzf mode410 411-*(Requires fzf 0.30.0 or above)*411+*(Requires fzf 0.36.0 or above)*412 413-fzf 0.30.0 added `rebind` action so we can "rebind" the bindings that were414-previously "unbound" via `unbind`.413+[fzf 0.30.0][0.30.0] added `rebind` action so we can "rebind" the bindings414+that were previously "unbound" via `unbind`.415 416 This is an improved version of the previous example that allows us to switch417 between Ripgrep launcher mode and fzf-only filtering mode via CTRL-R and418 CTRL-F.1 unmodified line420 ```sh421 #!/usr/bin/env bash422 423 # Switch between Ripgrep launcher mode (CTRL-R) and fzf filtering mode (CTRL-F)424+rm -f /tmp/rg-fzf-{r,f}425 RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "426 INITIAL_QUERY="${*:-}"427 IFS=: 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 ```444 445+- 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.450+451+[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#0360453+454 Log tailing455 -----------456 457 fzf can run long-running preview commands and render partial results before163 unmodified lines