summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2023-12-29 12:27:12 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2023-12-29 12:27:12 +0900
commit8d20f3d5c4e2645e623ed43487501dfe76a86b98 (patch)
tree48fa6b45cd29bc3a2e049a6dc0fa9b6714e9c60f
parent5d360180afdb059a30ac7912edf9bc94ea8d2fe3 (diff)
ADVANCED.md: Add toggling example with `transform` and `{fzf:prompt}`
Courtesy of @LangLangBart
-rw-r--r--ADVANCED.md29
1 files changed, 27 insertions, 2 deletions
diff --git a/ADVANCED.md b/ADVANCED.md
index 661bac11..535fdf1b 100644
--- a/ADVANCED.md
+++ b/ADVANCED.md
@@ -1,8 +1,8 @@
Advanced fzf examples
======================
-* *Last update: 2023/05/26*
-* *Requires fzf 0.41.0 or above*
+* *Last update: 2023/12/29*
+* *Requires fzf 0.45.0 or above*
---
@@ -16,6 +16,7 @@ Advanced fzf examples
* [Dynamic reloading of the list](#dynamic-reloading-of-the-list)
* [Updating the list of processes by pressing CTRL-R](#updating-the-list-of-processes-by-pressing-ctrl-r)
* [Toggling between data sources](#toggling-between-data-sources)
+ * [Toggling with a single key binding](#toggling-with-a-single-key-binding)
* [Ripgrep integration](#ripgrep-integration)
* [Using fzf as the secondary filter](#using-fzf-as-the-secondary-filter)
* [Using fzf as interactive Ripgrep launcher](#using-fzf-as-interactive-ripgrep-launcher)
@@ -208,6 +209,30 @@ find * | fzf --prompt 'All> ' \
![image](https://user-images.githubusercontent.com/700826/113465072-46321c00-946c-11eb-9b6f-cda3951df579.png)
+### Toggling with a single key binding
+
+The above example uses two different key bindings to toggle between two modes,
+but can we just use a single key binding?
+
+To make a key binding behave differently each time it is pressed, we need:
+
+1. a way to store the current state. i.e. "which mode are we in?"
+2. and a way to dynamically perform different actions depending on the state.
+
+The following example shows how to 1. store the current mode in the prompt
+string, 2. and use this information (`{fzf:prompt}`) to determine which
+actions to perform using the `transform` action.
+
+```sh
+fd --type file |
+ fzf --prompt 'Files> ' \
+ --header 'CTRL-T: Switch between Files/Directories' \
+ --bind 'ctrl-t:transform:[[ ! {fzf:prompt} =~ Files ]] &&
+ echo "change-prompt(Files> )+reload(fd --type file)" ||
+ echo "change-prompt(Directories> )+reload(fd --type directory)"' \
+ --preview '[[ {fzf:prompt} =~ Files ]] && bat --color=always {} || tree -C {}'
+```
+
Ripgrep integration
-------------------