]> git.rmz.io Git - dotfiles.git/blob - zsh/lib/fzf.zsh
b9188d6189e459fd90915a55a41bee12813c00f8
[dotfiles.git] / zsh / lib / fzf.zsh
1 if ! hash fzf 2>/dev/null; then
2 echo '`fzf` is not installed.'
3 return
4 fi
5
6 # Auto-completion
7 # ---------------
8 if [[ -f /usr/share/zsh/site-functions/_fzf ]]; then
9 source /usr/share/zsh/site-functions/_fzf
10 fi
11
12 # Key bindings
13 # ------------
14
15 # CTRL-T - Paste the selected file path(s) into the command line
16 __fsel() {
17 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
18 -o -type f -print \
19 -o -type d -print \
20 -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do
21 printf '%q ' "$item"
22 done
23 echo
24 }
25
26 if [[ $- =~ i ]]; then
27
28 if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
29 fzf-file-widget() {
30 local height
31 height=${FZF_TMUX_HEIGHT:-40%}
32 if [[ $height =~ %$ ]]; then
33 height="-p ${height%\%}"
34 else
35 height="-l $height"
36 fi
37 tmux split-window $height "cd $(printf %q "$PWD");zsh -c 'tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
38 }
39 else
40 fzf-file-widget() {
41 LBUFFER="${LBUFFER}$(__fsel)"
42 zle redisplay
43 }
44 fi
45 zle -N fzf-file-widget
46 bindkey '^T' fzf-file-widget
47
48 # ALT-C - cd into the selected directory
49 fzf-cd-widget() {
50 cd "${$(command find -L . -xdev \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
51 -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m):-.}"
52 zle reset-prompt
53 }
54 zle -N fzf-cd-widget
55 bindkey '\ec' fzf-cd-widget
56
57 # CTRL-R - Paste the selected command from history into the command line
58 fzf-history-widget() {
59 local selected
60 if selected=$(fc -l 1 | fzf -x +s --tac +m -n2..,.. --toggle-sort=ctrl-r -q "$LBUFFER"); then
61 num=$(echo "$selected" | head -1 | awk '{print $1}' | sed 's/[^0-9]//g')
62 LBUFFER=!$num
63 zle expand-history
64 fi
65 zle redisplay
66 }
67 zle -N fzf-history-widget
68 bindkey '^R' fzf-history-widget
69
70 # ALT-I - Paste the selected entry from locate output into the command line
71 fzf-locate-widget() {
72 local selected
73 if selected=$(locate / | fzf -q "$LBUFFER"); then
74 LBUFFER=$selected
75 fi
76 zle redisplay
77 }
78 zle -N fzf-locate-widget
79 bindkey '\ei' fzf-locate-widget
80
81 fi