summaryrefslogtreecommitdiffstats
path: root/computer/tools/fzf.md
blob: 0088f2fb638029fa1085a88c4aa2a7179bb39564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: FZF
---

[fzf](https://github.com/junegunn/fzf)
is a fuzzy selection tool written in [[go]]. An alternative in [[rust]] would be
[skim](https://github.com/lotabout/skim).

[[fzf]] can be used to automate a lot of things, for example it can replace the
`CTRL-R` shortcut in bash:

```bash
## Include in your .bashrc:
source PATH_TO_FZF_CLONE/shell/key-bindings.bash
```

And now, `CTRL-R` will fuzzy search the history.

`fzf` is a huge productivity boost for commandline work and even other tools
where it integrates.

Some more examples:

```bash
## fco - fuzzy checkout git branch
fco() {
    local tags branches target
    local fzf_cmd="fzf"
    tags=$(git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') \
        || return

    branches=$(git branch --all | grep -v HEAD             | \
               sed "s/.* //"    | sed "s#remotes/[^/]*/##" | \
               sort -u          | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') \
        || return

    [[ $TMUX ]] && [[ $(which fzf-tmux 2>/dev/null) ]] && \
        fzf_cmd="fzf-tmux -l30 -- "

    target=$( (echo "$tags"; echo "$branches") | \
               $fzf_cmd --no-hscroll --ansi +m -d "\t" -n 2) \
        || return

    git checkout $(echo "$target" | awk '{print $2}')
}

## Fuzzy foreground, if you use `CTRL-Z` a lot
fj() {
    [ $(jobs | wc -l) -eq 0 ] && return
    local j=$(jobs | awk '{ print $3, " - ", $2, " - ", $1; }' | fzf -1 -0)
    echo $j
    fg $(echo $j | sed -r 's/(.*)\[([0-9]*).*/\2/g')
}

## Fuzzy select the right tmux session, even within a tmux session
fmux() {
    local session
    session=$(tmux list-sessions -F "#{session_name}" | \
        fzf --query="$1" --select-1 --exit-0)

    if [[ -z "${TMUX}" ]]; then
        tmux -2 attach -t "$session"
    else
        tmux -2 switch-client -t "$session"
    fi
}
```

## fzf in vim

It can also be integrated in `vim` for fuzzy selecting. Things like
fuzzy-jump-to-line, for example:

```viml
" Using the fzf-preview plugin, this can even be enhanced with a preview window:
nmap <Leader>zf :FZF<CR>
nmap <Leader>zr :FZFRg<CR>
nmap <Leader>zh :FZFHistory<CR>
nmap <Leader>zl :FZFBLines<CR>
nmap <Leader>zt :FZFTags<CR>
nmap <Leader>zT :FZFBTags<CR>
nmap <Leader>zm :FZFMarks<CR>
nmap <Leader>zw :FZFWindows<CR>
```