Debian Codegraph @

ADVANCED.md

raw
1 Advanced fzf examples
2 ======================
3
4 * *Last update: 2025/02/02*
5 * *Requires fzf 0.59.0 or later*
6
7 ---
8
9 <!-- vim-markdown-toc GFM -->
10
11 * [Introduction](#introduction)
12 * [Display modes](#display-modes)
13 * [`--height`](#--height)
14 * [`--tmux`](#--tmux)
15 * [Dynamic reloading of the list](#dynamic-reloading-of-the-list)
16 * [Updating the list of processes by pressing CTRL-R](#updating-the-list-of-processes-by-pressing-ctrl-r)
17 * [Toggling between data sources](#toggling-between-data-sources)
18 * [Toggling with a single key binding](#toggling-with-a-single-key-binding)
19 * [Ripgrep integration](#ripgrep-integration)
20 * [Using fzf as the secondary filter](#using-fzf-as-the-secondary-filter)
21 * [Using fzf as interactive Ripgrep launcher](#using-fzf-as-interactive-ripgrep-launcher)
22 * [Switching to fzf-only search mode](#switching-to-fzf-only-search-mode)
23 * [Switching between Ripgrep mode and fzf mode](#switching-between-ripgrep-mode-and-fzf-mode)
24 * [Switching between Ripgrep mode and fzf mode using a single key binding](#switching-between-ripgrep-mode-and-fzf-mode-using-a-single-key-binding)
25 * [Controlling Ripgrep search and fzf search simultaneously](#controlling-ripgrep-search-and-fzf-search-simultaneously)
26 * [Log tailing](#log-tailing)
27 * [Key bindings for git objects](#key-bindings-for-git-objects)
28 * [Files listed in `git status`](#files-listed-in-git-status)
29 * [Branches](#branches)
30 * [Commit hashes](#commit-hashes)
31 * [Color themes](#color-themes)
32 * [fzf Theme Playground](#fzf-theme-playground)
33 * [Generating fzf color theme from Vim color schemes](#generating-fzf-color-theme-from-vim-color-schemes)
34
35 <!-- vim-markdown-toc -->
36
37 Introduction
38 ------------
39
40 fzf is an interactive [Unix filter][filter] program that is designed to be
41 used with other Unix tools. It reads a list of items from the standard input,
42 allows you to select a subset of the items, and prints the selected ones to
43 the standard output. You can think of it as an interactive version of *grep*,
44 and it's already useful even if you don't know any of its options.
45
46 ```sh
47 # 1. ps: Feed the list of processes to fzf
48 # 2. fzf: Interactively select a process using fuzzy matching algorithm
49 # 3. awk: Take the PID from the selected line
50 # 3. kill: Kill the process with the PID
51 ps -ef | fzf | awk '{print $2}' | xargs kill -9
52 ```
53
54 [filter]: https://en.wikipedia.org/wiki/Filter_(software)
55
56 While the above example succinctly summarizes the fundamental concept of fzf,
57 you can build much more sophisticated interactive workflows using fzf once you
58 learn its wide variety of features.
59
60 - To see the full list of options and features, see `man fzf`
61 - To see the latest additions, see [CHANGELOG.md](CHANGELOG.md)
62
63 This document will guide you through some examples that will familiarize you
64 with the advanced features of fzf.
65
66 Display modes
67 -------------
68
69 ### `--height`
70
71 fzf by default opens in fullscreen mode, but it's not always desirable.
72 Oftentimes, you want to see the current context of the terminal while using
73 fzf. `--height` is an option for opening fzf below the cursor in
74 non-fullscreen mode so you can still see the previous commands and their
75 results above it.
76
77 ```sh
78 fzf --height=40%
79 ```
80
81 ![image](https://user-images.githubusercontent.com/700826/113379893-c184c680-93b5-11eb-9676-c7c0a2f01748.png)
82
83 You might also want to experiment with other layout options such as
84 `--layout=reverse`, `--info=inline`, `--border`, `--margin`, etc.
85
86 ```sh
87 fzf --height=40% --layout=reverse
88 fzf --height=40% --layout=reverse --info=inline
89 fzf --height=40% --layout=reverse --info=inline --border
90 fzf --height=40% --layout=reverse --info=inline --border --margin=1
91 fzf --height=40% --layout=reverse --info=inline --border --margin=1 --padding=1
92 ```
93
94 ![image](https://user-images.githubusercontent.com/700826/113379932-dfeac200-93b5-11eb-9e28-df1a2ee71f90.png)
95
96 *(See man page to see the full list of options)*
97
98 But you definitely don't want to repeat `--height=40% --layout=reverse
99 --info=inline --border --margin=1 --padding=1` every time you use fzf. You
100 could write a wrapper script or shell alias, but there is an easier option.
101 Define `$FZF_DEFAULT_OPTS` like so:
102
103 ```sh
104 export FZF_DEFAULT_OPTS="--height=40% --layout=reverse --info=inline --border --margin=1 --padding=1"
105 ```
106
107 ### `--tmux`
108
109 (Requires tmux 3.3 or later)
110
111 If you're using tmux, you can open fzf in a tmux popup using `--tmux` option.
112
113 ```sh
114 # Open fzf in a tmux popup at the center of the screen with 70% width and height
115 fzf --tmux 70%
116 ```
117
118 ![image](https://github.com/junegunn/fzf/assets/700826/9c365405-c700-49b2-8985-60d822ed4cff)
119
120 `--tmux` option is silently ignored if you're not on tmux. So if you're trying
121 to avoid opening fzf in fullscreen, try specifying both `--height` and `--tmux`.
122
123 ```sh
124 # --tmux is specified later so it takes precedence over --height when on tmux.
125 # If you're not on tmux, --tmux is ignored and --height is used instead.
126 fzf --height 70% --tmux 70%
127 ```
128
129 You can also specify the position, width, and height of the popup window in
130 the following format:
131
132 * `[center|top|bottom|left|right][,SIZE[%]][,SIZE[%][,border-native]]`
133
134 ```sh
135 # 100% width and 60% height
136 fzf --tmux 100%,60% --border horizontal
137 ```
138
139 ![image](https://github.com/junegunn/fzf/assets/700826/f80d3514-d69f-42f2-a8de-a392a562bfcf)
140
141 ```sh
142 # On the right (50% width)
143 fzf --tmux right
144 ```
145
146 ![image](https://github.com/junegunn/fzf/assets/700826/4033ade4-7efa-421b-a3fb-a430d197098a)
147
148 ```sh
149 # On the left (40% width and 70% height)
150 fzf --tmux left,40%,70%
151 ```
152
153 ![image](https://github.com/junegunn/fzf/assets/700826/efe43881-2bf0-49ea-ab2e-1377f778cd52)
154
155 > [!TIP]
156 > You might also want to check out my tmux plugins which support this popup
157 > window layout.
158 >
159 > - https://github.com/junegunn/tmux-fzf-url
160 > - https://github.com/junegunn/tmux-fzf-maccy
161
162 Dynamic reloading of the list
163 -----------------------------
164
165 fzf can dynamically update the candidate list using an arbitrary program with
166 `reload` bindings (The design document for `reload` can be found
167 [here][reload]).
168
169 [reload]: https://github.com/junegunn/fzf/issues/1750
170
171 ### Updating the list of processes by pressing CTRL-R
172
173 This example shows how you can set up a binding for dynamically updating the
174 list without restarting fzf.
175
176 ```sh
177 (date; ps -ef) |
178 fzf --bind='ctrl-r:reload(date; ps -ef)' \
179 --header=$'Press CTRL-R to reload\n\n' --header-lines=2 \
180 --preview='echo {}' --preview-window=down,3,wrap \
181 --layout=reverse --height=80% | awk '{print $2}' | xargs kill -9
182 ```
183
184 ![image](https://user-images.githubusercontent.com/700826/113465047-200c7c00-946c-11eb-918c-268f37a900c8.png)
185
186 - The initial command is `(date; ps -ef)`. It prints the current date and
187 time, and the list of the processes.
188 - With `--header` option, you can show any message as the fixed header.
189 - To disallow selecting the first two lines (`date` and `ps` header), we use
190 `--header-lines=2` option.
191 - `--bind='ctrl-r:reload(date; ps -ef)'` binds CTRL-R to `reload` action that
192 runs `date; ps -ef`, so we can update the list of the processes by pressing
193 CTRL-R.
194 - We use simple `echo {}` preview option, so we can see the entire line on the
195 preview window below even if it's too long
196
197 ### Toggling between data sources
198
199 You're not limited to just one reload binding. Set up multiple bindings so
200 you can switch between data sources.
201
202 ```sh
203 find * | fzf --prompt 'All> ' \
204 --header 'CTRL-D: Directories / CTRL-F: Files' \
205 --bind 'ctrl-d:change-prompt(Directories> )+reload(find * -type d)' \
206 --bind 'ctrl-f:change-prompt(Files> )+reload(find * -type f)'
207 ```
208
209 ![image](https://user-images.githubusercontent.com/700826/113465073-4af6d000-946c-11eb-858f-2372c0955f67.png)
210
211 ![image](https://user-images.githubusercontent.com/700826/113465072-46321c00-946c-11eb-9b6f-cda3951df579.png)
212
213 ### Toggling with a single key binding
214
215 The above example uses two different key bindings to toggle between two modes,
216 but can we just use a single key binding?
217
218 To make a key binding behave differently each time it is pressed, we need:
219
220 1. a way to store the current state. i.e. "which mode are we in?"
221 2. and a way to dynamically perform different actions depending on the state.
222
223 The following example shows how to 1. store the current mode in the prompt
224 string, 2. and use this information (`$FZF_PROMPT`) to determine which
225 actions to perform using the `transform` action.
226
227 ```sh
228 fd --type file |
229 fzf --prompt 'Files> ' \
230 --header 'CTRL-T: Switch between Files/Directories' \
231 --bind 'ctrl-t:transform:[[ ! $FZF_PROMPT =~ Files ]] &&
232 echo "change-prompt(Files> )+reload(fd --type file)" ||
233 echo "change-prompt(Directories> )+reload(fd --type directory)"' \
234 --preview '[[ $FZF_PROMPT =~ Files ]] && bat --color=always {} || tree -C {}'
235 ```
236
237 Ripgrep integration
238 -------------------
239
240 ### Using fzf as the secondary filter
241
242 * Requires [bat][bat]
243 * Requires [Ripgrep][rg]
244
245 [bat]: https://github.com/sharkdp/bat
246 [rg]: https://github.com/BurntSushi/ripgrep
247
248 fzf is pretty fast for filtering a list that you will rarely have to think
249 about its performance. But it is not the right tool for searching for text
250 inside many large files, and in that case you should definitely use something
251 like [Ripgrep][rg].
252
253 In the next example, Ripgrep is the primary filter that searches for the given
254 text in files, and fzf is used as the secondary fuzzy filter that adds
255 interactivity to the workflow. And we use [bat][bat] to show the matching line in
256 the preview window.
257
258 This is a bash script and it will not run as expected on other non-compliant
259 shells. To avoid the compatibility issue, let's save this snippet as a script
260 file called `rfv`.
261
262 ```bash
263 #!/usr/bin/env bash
264
265 # 1. Search for text in files using Ripgrep
266 # 2. Interactively narrow down the list using fzf
267 # 3. Open the file in Vim
268 rg --color=always --line-number --no-heading --smart-case "${*:-}" |
269 fzf --ansi \
270 --color "hl:-1:underline,hl+:-1:underline:reverse" \
271 --delimiter : \
272 --preview 'bat --color=always {1} --highlight-line {2}' \
273 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
274 --bind 'enter:become(vim {1} +{2})'
275 ```
276
277 And run it with an initial query string.
278
279 ```sh
280 # Make the script executable
281 chmod +x rfv
282
283 # Run it with the initial query "algo"
284 ./rfv algo
285 ```
286
287 > Ripgrep will perform the initial search and list all the lines that contain
288 `algo`. Then we further narrow down the list on fzf.
289
290 ![image](https://user-images.githubusercontent.com/700826/113683873-a42a6200-96ff-11eb-9666-26ce4091b0e4.png)
291
292 I know it's a lot to digest, let's try to break down the code.
293
294 - Ripgrep prints the matching lines in the following format
295 ```
296 man/man1/fzf.1:54:.BI "--algo=" TYPE
297 man/man1/fzf.1:55:Fuzzy matching algorithm (default: v2)
298 man/man1/fzf.1:58:.BR v2 " Optimal scoring algorithm (quality)"
299 src/pattern_test.go:7: "github.com/junegunn/fzf/src/algo"
300 ```
301 The first token delimited by `:` is the file path, and the second token is
302 the line number of the matching line. They respectively correspond to `{1}`
303 and `{2}` in the preview command.
304 - `--preview 'bat --color=always {1} --highlight-line {2}'`
305 - As we run `rg` with `--color=always` option, we should tell fzf to parse
306 ANSI color codes in the input by setting `--ansi`.
307 - We customize how fzf colors various text elements using `--color` option.
308 `-1` tells fzf to keep the original color from the input. See `man fzf` for
309 available color options.
310 - The value of `--preview-window` option consists of 5 components delimited
311 by `,`
312 1. `up` -- Position of the preview window
313 1. `60%` -- Size of the preview window
314 1. `border-bottom` -- Preview window border only on the bottom side
315 1. `+{2}+3/3` -- Scroll offset of the preview contents
316 1. `~3` -- Fixed header
317 - Let's break down the latter two. We want to display the bat output in the
318 preview window with a certain scroll offset so that the matching line is
319 positioned near the center of the preview window.
320 - `+{2}` -- The base offset is extracted from the second token
321 - `+3` -- We add 3 lines to the base offset to compensate for the header
322 part of `bat` output
323 - ```
324 ───────┬──────────────────────────────────────────────────────────
325 │ File: CHANGELOG.md
326 ───────┼──────────────────────────────────────────────────────────
327 1 │ CHANGELOG
328 2 │ =========
329 3 │
330 4 │ 0.26.0
331 5 │ ------
332 ```
333 - `/3` adjusts the offset so that the matching line is shown at a third
334 position in the window
335 - `~3` makes the top three lines fixed header so that they are always
336 visible regardless of the scroll offset
337 - Instead of using shell script to process the final output of fzf, we use
338 `become(...)` action which was added in [fzf 0.38.0][0.38.0] to turn fzf
339 into a new process that opens the file with `vim` (`vim {1}`) and move the
340 cursor to the line (`+{2}`).
341
342 [0.38.0]: https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0380
343
344 ### Using fzf as interactive Ripgrep launcher
345
346 We have learned that we can bind `reload` action to a key (e.g.
347 `--bind=ctrl-r:execute(ps -ef)`). In the next example, we are going to **bind
348 `reload` action to `change` event** so that whenever the user *changes* the
349 query string on fzf, `reload` action is triggered.
350
351 Here is a variation of the above `rfv` script. fzf will restart Ripgrep every
352 time the user updates the query string on fzf. Searching and filtering is
353 completely done by Ripgrep, and fzf merely provides the interactive interface.
354 So we lose the "fuzziness", but the performance will be better on larger
355 projects, and it will free up memory as you narrow down the results.
356
357 ```bash
358 #!/usr/bin/env bash
359
360 # 1. Search for text in files using Ripgrep
361 # 2. Interactively restart Ripgrep with reload action
362 # 3. Open the file in Vim
363 RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
364 INITIAL_QUERY="${*:-}"
365 fzf --ansi --disabled --query "$INITIAL_QUERY" \
366 --bind "start:reload:$RG_PREFIX {q} || true" \
367 --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
368 --delimiter : \
369 --preview 'bat --color=always {1} --highlight-line {2}' \
370 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
371 --bind 'enter:become(vim {1} +{2})'
372 ```
373
374 ![image](https://user-images.githubusercontent.com/700826/113684212-f9ff0a00-96ff-11eb-8737-7bb571d320cc.png)
375
376 - Instead of starting fzf in the usual `rg ... | fzf` form, we make it start
377 the initial Ripgrep process immediately via `start:reload` binding for the
378 consistency of the code.
379 - Filtering is no longer a responsibility of fzf; hence `--disabled`
380 - `{q}` in the reload command evaluates to the query string on fzf prompt.
381 - `sleep 0.1` in the reload command is for "debouncing". This small delay will
382 reduce the number of intermediate Ripgrep processes while we're typing in
383 a query.
384
385 ### Switching to fzf-only search mode
386
387 In the previous example, we lost fuzzy matching capability as we completely
388 delegated search functionality to Ripgrep. But we can dynamically switch to
389 fzf-only search mode by *"unbinding"* `reload` action from `change` event.
390
391 ```sh
392 #!/usr/bin/env bash
393
394 # Two-phase filtering with Ripgrep and fzf
395 #
396 # 1. Search for text in files using Ripgrep
397 # 2. Interactively restart Ripgrep with reload action
398 # * Press alt-enter to switch to fzf-only filtering
399 # 3. Open the file in Vim
400 RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
401 INITIAL_QUERY="${*:-}"
402 fzf --ansi --disabled --query "$INITIAL_QUERY" \
403 --bind "start:reload:$RG_PREFIX {q}" \
404 --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
405 --bind "alt-enter:unbind(change,alt-enter)+change-prompt(2. fzf> )+enable-search+clear-query" \
406 --color "hl:-1:underline,hl+:-1:underline:reverse" \
407 --prompt '1. ripgrep> ' \
408 --delimiter : \
409 --preview 'bat --color=always {1} --highlight-line {2}' \
410 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
411 --bind 'enter:become(vim {1} +{2})'
412 ```
413
414 * Phase 1. Filtering with Ripgrep
415 ![image](https://user-images.githubusercontent.com/700826/119213880-735e8a80-bafd-11eb-8493-123e4be24fbc.png)
416 * Phase 2. Filtering with fzf
417 ![image](https://user-images.githubusercontent.com/700826/119213887-7e191f80-bafd-11eb-98c9-71a1af9d7aab.png)
418
419 - We added `--prompt` option to show that fzf is initially running in "Ripgrep
420 launcher mode".
421 - We added `alt-enter` binding that
422 1. unbinds `change` event, so Ripgrep is no longer restarted on key press
423 2. changes the prompt to `2. fzf>`
424 3. enables search functionality of fzf
425 4. clears the current query string that was used to start Ripgrep process
426 5. and unbinds `alt-enter` itself as this is a one-off event
427 - We reverted `--color` option for customizing how the matching chunks are
428 displayed in the second phase
429
430 ### Switching between Ripgrep mode and fzf mode
431
432 [fzf 0.30.0][0.30.0] added `rebind` action so we can "rebind" the bindings
433 that were previously "unbound" via `unbind`.
434
435 This is an improved version of the previous example that allows us to switch
436 between Ripgrep launcher mode and fzf-only filtering mode via CTRL-R and
437 CTRL-F.
438
439 ```sh
440 #!/usr/bin/env bash
441
442 # Switch between Ripgrep launcher mode (CTRL-R) and fzf filtering mode (CTRL-F)
443 rm -f /tmp/rg-fzf-{r,f}
444 RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
445 INITIAL_QUERY="${*:-}"
446 fzf --ansi --disabled --query "$INITIAL_QUERY" \
447 --bind "start:reload($RG_PREFIX {q})+unbind(ctrl-r)" \
448 --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
449 --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)" \
450 --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)" \
451 --color "hl:-1:underline,hl+:-1:underline:reverse" \
452 --prompt '1. ripgrep> ' \
453 --delimiter : \
454 --header '╱ CTRL-R (ripgrep mode) ╱ CTRL-F (fzf mode) ╱' \
455 --preview 'bat --color=always {1} --highlight-line {2}' \
456 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
457 --bind 'enter:become(vim {1} +{2})'
458 ```
459
460 - To restore the query string when switching between modes, we store the
461 current query in `/tmp/rg-fzf-{r,f}` files and restore the query using
462 `transform-query` action which was added in [fzf 0.36.0][0.36.0].
463 - Also note that we unbind `ctrl-r` binding on `start` event which is
464 triggered once when fzf starts.
465
466 [0.30.0]: https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0300
467 [0.36.0]: https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0360
468
469 ### Switching between Ripgrep mode and fzf mode using a single key binding
470
471 In contrast to the previous version, we use just one hotkey to toggle between
472 ripgrep and fzf mode. This is achieved by using the `$FZF_PROMPT` as a state
473 within the `transform` action, a feature introduced in [fzf 0.45.0][0.45.0]. A
474 more detailed explanation of this feature can be found in a previous section -
475 [Toggling with a single keybinding](#toggling-with-a-single-key-binding).
476
477 [0.45.0]: https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0450
478
479 When using the `transform` action, the placeholder (`\{q}`) should be escaped to
480 prevent immediate evaluation.
481
482 ```sh
483 #!/usr/bin/env bash
484
485 # Switch between Ripgrep mode and fzf filtering mode (CTRL-T)
486 rm -f /tmp/rg-fzf-{r,f}
487 RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
488 INITIAL_QUERY="${*:-}"
489 fzf --ansi --disabled --query "$INITIAL_QUERY" \
490 --bind "start:reload:$RG_PREFIX {q}" \
491 --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
492 --bind 'ctrl-t:transform:[[ ! $FZF_PROMPT =~ ripgrep ]] &&
493 echo "rebind(change)+change-prompt(1. ripgrep> )+disable-search+transform-query:echo \{q} > /tmp/rg-fzf-f; cat /tmp/rg-fzf-r" ||
494 echo "unbind(change)+change-prompt(2. fzf> )+enable-search+transform-query:echo \{q} > /tmp/rg-fzf-r; cat /tmp/rg-fzf-f"' \
495 --color "hl:-1:underline,hl+:-1:underline:reverse" \
496 --prompt '1. ripgrep> ' \
497 --delimiter : \
498 --header 'CTRL-T: Switch between ripgrep/fzf' \
499 --preview 'bat --color=always {1} --highlight-line {2}' \
500 --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
501 --bind 'enter:become(vim {1} +{2})'
502 ```
503
504 ### Controlling Ripgrep search and fzf search simultaneously
505
506 `search` and `transform-search` action allow you to trigger an fzf search with
507 an arbitrary query string. This frees fzf from strictly following the prompt
508 input, enabling custom search syntax.
509
510 In the example below, `transform` action is used to conditionally trigger
511 `reload` for ripgrep, followed by `search` for fzf. The first word of the
512 query initiates the Ripgrep process to generate the initial results, while the
513 remainder of the query is passed to fzf for secondary filtering.
514
515 ```sh
516 #!/usr/bin/env bash
517
518 export TEMP=$(mktemp -u)
519 trap 'rm -f "$TEMP"' EXIT
520
521 INITIAL_QUERY="${*:-}"
522 TRANSFORMER='
523 rg_pat={q:1} # The first word is passed to ripgrep
524 fzf_pat={q:2..} # The rest are passed to fzf
525
526 if ! [[ -r "$TEMP" ]] || [[ $rg_pat != $(cat "$TEMP") ]]; then
527 echo "$rg_pat" > "$TEMP"
528 printf "reload:sleep 0.1; rg --column --line-number --no-heading --color=always --smart-case %q || true" "$rg_pat"
529 fi
530 echo "+search:$fzf_pat"
531 '
532 fzf --ansi --disabled --query "$INITIAL_QUERY" \
533 --with-shell 'bash -c' \
534 --bind "start,change:transform:$TRANSFORMER" \
535 --color "hl:-1:underline,hl+:-1:underline:reverse" \
536 --delimiter : \
537 --preview 'bat --color=always {1} --highlight-line {2}' \
538 --preview-window 'up,60%,border-line,+{2}+3/3,~3' \
539 --bind 'enter:become(vim {1} +{2})'
540 ```
541
542 Log tailing
543 -----------
544
545 fzf can run long-running preview commands and render partial results before
546 completion. And when you specify `follow` flag in `--preview-window` option,
547 fzf will "`tail -f`" the result, automatically scrolling to the bottom.
548
549 ```bash
550 # With "follow", preview window will automatically scroll to the bottom.
551 # "\033[2J" is an ANSI escape sequence for clearing the screen.
552 # When fzf reads this code it clears the previous preview contents.
553 fzf --preview-window follow --preview 'for i in $(seq 100000); do
554 echo "$i"
555 sleep 0.01
556 (( i % 300 == 0 )) && printf "\033[2J"
557 done'
558 ```
559
560 ![image](https://user-images.githubusercontent.com/700826/113473303-dd669600-94a3-11eb-88a9-1f61b996bb0e.png)
561
562 Admittedly, that was a silly example. Here's a practical one for browsing
563 Kubernetes pods.
564
565 ```bash
566 pods() {
567 command='kubectl get pods --all-namespaces' fzf \
568 --info=inline --layout=reverse --header-lines=1 \
569 --prompt "$(kubectl config current-context | sed 's/-context$//')> " \
570 --header $'╱ Enter (kubectl exec) ╱ CTRL-O (open log in editor) ╱ CTRL-R (reload) ╱\n\n' \
571 --bind 'start,ctrl-r:reload:$command' \
572 --bind 'ctrl-/:change-preview-window(80%,border-bottom|hidden|)' \
573 --bind 'enter:execute:kubectl exec -it --namespace {1} {2} -- bash' \
574 --bind 'ctrl-o:execute:${EDITOR:-vim} <(kubectl logs --all-containers --namespace {1} {2})' \
575 --preview-window up:follow \
576 --preview 'kubectl logs --follow --all-containers --tail=10000 --namespace {1} {2}' "$@"
577 }
578 ```
579
580 ![image](https://user-images.githubusercontent.com/700826/113473547-1d7a4880-94a5-11eb-98ef-9aa6f0ed215a.png)
581
582 - The preview window will *"log tail"* the pod
583 - Holding on to a large amount of log will consume a lot of memory. So we
584 limited the initial log amount with `--tail=10000`.
585 - `execute` bindings allow you to run any command without leaving fzf
586 - Press enter key on a pod to `kubectl exec` into it
587 - Press CTRL-O to open the log in your editor
588 - Press CTRL-R to reload the pod list
589 - Press CTRL-/ repeatedly to rotate through a different sets of preview
590 window options
591 1. `80%,border-bottom`
592 1. `hidden`
593 1. Empty string after `|` translates to the default options from `--preview-window`
594
595 Key bindings for git objects
596 ----------------------------
597
598 Oftentimes, you want to put the identifiers of various Git object to the
599 command-line. For example, it is common to write commands like these:
600
601 ```sh
602 git checkout [SOME_COMMIT_HASH or BRANCH or TAG]
603 git diff [SOME_COMMIT_HASH or BRANCH or TAG] [SOME_COMMIT_HASH or BRANCH or TAG]
604 ```
605
606 [fzf-git.sh](https://github.com/junegunn/fzf-git.sh) project defines a set of
607 fzf-based key bindings for Git objects. I strongly recommend that you check
608 them out because they are seriously useful.
609
610 ### Files listed in `git status`
611
612 <kbd>CTRL-G</kbd><kbd>CTRL-F</kbd>
613
614 ![image](https://user-images.githubusercontent.com/700826/113473779-a9d93b00-94a6-11eb-87b5-f62a8d0a0efc.png)
615
616 ### Branches
617
618 <kbd>CTRL-G</kbd><kbd>CTRL-B</kbd>
619
620 ![image](https://user-images.githubusercontent.com/700826/113473758-87dfb880-94a6-11eb-82f4-9218103f10bd.png)
621
622 ### Commit hashes
623
624 <kbd>CTRL-G</kbd><kbd>CTRL-H</kbd>
625
626 ![image](https://user-images.githubusercontent.com/700826/113473765-91692080-94a6-11eb-8d38-ed4d41f27ac1.png)
627
628 Color themes
629 ------------
630
631 You can customize how fzf colors the text elements with `--color` option. Here
632 are a few color themes. Note that you need a terminal emulator that can
633 display 24-bit colors.
634
635 ```sh
636 # junegunn/seoul256.vim (dark)
637 export FZF_DEFAULT_OPTS='--color=bg+:#3F3F3F,bg:#4B4B4B,border:#6B6B6B,spinner:#98BC99,hl:#719872,fg:#D9D9D9,header:#719872,info:#BDBB72,pointer:#E12672,marker:#E17899,fg+:#D9D9D9,preview-bg:#3F3F3F,prompt:#98BEDE,hl+:#98BC99'
638 ```
639
640 ![seoul256](https://user-images.githubusercontent.com/700826/113475011-2c192d80-94ae-11eb-9d17-1e5867bae01f.png)
641
642 ```sh
643 # junegunn/seoul256.vim (light)
644 export FZF_DEFAULT_OPTS='--color=bg+:#D9D9D9,bg:#E1E1E1,border:#C8C8C8,spinner:#719899,hl:#719872,fg:#616161,header:#719872,info:#727100,pointer:#E12672,marker:#E17899,fg+:#616161,preview-bg:#D9D9D9,prompt:#0099BD,hl+:#719899'
645 ```
646
647 ![seoul256-light](https://user-images.githubusercontent.com/700826/113475022-389d8600-94ae-11eb-905f-0939dd535837.png)
648
649 ```sh
650 # morhetz/gruvbox
651 export FZF_DEFAULT_OPTS='--color=bg+:#3c3836,bg:#32302f,spinner:#fb4934,hl:#928374,fg:#ebdbb2,header:#928374,info:#8ec07c,pointer:#fb4934,marker:#fb4934,fg+:#ebdbb2,prompt:#fb4934,hl+:#fb4934'
652 ```
653
654 ![gruvbox](https://user-images.githubusercontent.com/700826/113475042-494dfc00-94ae-11eb-9322-cd03a027305a.png)
655
656 ```sh
657 # arcticicestudio/nord-vim
658 export FZF_DEFAULT_OPTS='--color=bg+:#3B4252,bg:#2E3440,spinner:#81A1C1,hl:#616E88,fg:#D8DEE9,header:#616E88,info:#81A1C1,pointer:#81A1C1,marker:#81A1C1,fg+:#D8DEE9,prompt:#81A1C1,hl+:#81A1C1'
659 ```
660
661 ![nord](https://user-images.githubusercontent.com/700826/113475063-67b3f780-94ae-11eb-9b24-5f0d22b63399.png)
662
663 ```sh
664 # tomasr/molokai
665 export FZF_DEFAULT_OPTS='--color=bg+:#293739,bg:#1B1D1E,border:#808080,spinner:#E6DB74,hl:#7E8E91,fg:#F8F8F2,header:#7E8E91,info:#A6E22E,pointer:#A6E22E,marker:#F92672,fg+:#F8F8F2,prompt:#F92672,hl+:#F92672'
666 ```
667
668 ![molokai](https://user-images.githubusercontent.com/700826/113475085-8619f300-94ae-11eb-85e4-2766fc3246bf.png)
669
670 ### fzf Theme Playground
671
672 [fzf Theme Playground](https://vitormv.github.io/fzf-themes/) created by
673 [Vitor Mello](https://github.com/vitormv) is a webpage where you can
674 interactively create fzf themes.
675
676 ### Generating fzf color theme from Vim color schemes
677
678 The Vim plugin of fzf can generate `--color` option from the current color
679 scheme according to `g:fzf_colors` variable. You can find the detailed
680 explanation [here](https://github.com/junegunn/fzf/blob/master/README-VIM.md#explanation-of-gfzf_colors).
681
682 Here is an example. Add this to your Vim configuration file.
683
684 ```vim
685 let g:fzf_colors =
686 \ { 'fg': ['fg', 'Normal'],
687 \ 'bg': ['bg', 'Normal'],
688 \ 'preview-bg': ['bg', 'NormalFloat'],
689 \ 'hl': ['fg', 'Comment'],
690 \ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
691 \ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
692 \ 'hl+': ['fg', 'Statement'],
693 \ 'info': ['fg', 'PreProc'],
694 \ 'border': ['fg', 'Ignore'],
695 \ 'prompt': ['fg', 'Conditional'],
696 \ 'pointer': ['fg', 'Exception'],
697 \ 'marker': ['fg', 'Keyword'],
698 \ 'spinner': ['fg', 'Label'],
699 \ 'header': ['fg', 'Comment'] }
700 ```
701
702 Then you can see how the `--color` option is generated by printing the result
703 of `fzf#wrap()`.
704
705 ```vim
706 :echo fzf#wrap()
707 ```
708
709 Use this command to append `export FZF_DEFAULT_OPTS="..."` line to the end of
710 the current file.
711
712 ```vim
713 :call append('$', printf('export FZF_DEFAULT_OPTS="%s"', matchstr(fzf#wrap().options, "--color[^']*")))
714 ```