summaryrefslogtreecommitdiffstats
path: root/install
blob: 970b15a90f2f44fcc384fedf6f970625c1f57951 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash

cd `dirname $BASH_SOURCE`
fzf_base=`pwd`

# ruby executable
echo -n "Checking Ruby executable ... "
ruby=`which ruby`
if [ $? -ne 0 ]; then
  echo "ruby executable not found!"
  exit 1
fi
echo "OK"

# Curses-support
echo -n "Checking Curses support ... "
/usr/bin/env ruby -e "begin; require 'curses'; rescue Exception; exit 1; end"
if [ $? -eq 0 ]; then
  echo "OK"
else
  echo "Not found"
  echo "Installing 'curses' gem ... "
  /usr/bin/env gem install curses
  if [ $? -ne 0 ]; then
    echo "Failed to install 'curses' gem."
    echo "Try installing it as root: sudo gem install curses"
    exit 1
  fi
fi

# Ruby version
echo -n "Checking Ruby version ... "
/usr/bin/env ruby -e 'exit RUBY_VERSION >= "1.9"'
if [ $? -eq 0 ]; then
  echo ">= 1.9"
  /usr/bin/env ruby --disable-gems -e "begin; require 'curses'; rescue Exception; exit 1; end"
  if [ $? -eq 0 ]; then
    fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
  else
    fzf_cmd="$ruby $fzf_base/fzf"
  fi
else
  echo "< 1.9"
  fzf_cmd="$ruby $fzf_base/fzf"
fi

# Auto-completion
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
echo
[[ ! $REPLY =~ ^[Nn]$ ]]
auto_completion=$?

# Key-bindings
read -p "Do you want to add key bindings? ([y]/n) " -n 1 -r
echo
[[ ! $REPLY =~ ^[Nn]$ ]]
key_bindings=$?

echo
for shell in bash zsh; do
  echo -n "Generate ~/.fzf.$shell ... "
  src=~/.fzf.${shell}

  fzf_completion="source $fzf_base/fzf-completion.${shell}"
  if [ $auto_completion -ne 0 ]; then
    fzf_completion="# $fzf_completion"
  fi

  cat > $src << EOF
# Setup fzf function
# ------------------
unalias fzf 2> /dev/null
fzf() {
  $fzf_cmd "\$@"
}
export -f fzf > /dev/null

# Auto-completion
# ---------------
$fzf_completion

EOF

  if [ $key_bindings -eq 0 ]; then
    if [ $shell = bash ]; then
      cat >> $src << "EOF"
# Key bindings
# ------------
# Required to refresh the prompt after fzf
bind '"\er": redraw-current-line'

# CTRL-T - Paste the selected file path into the command line
__fsel() {
  find * -path '*/\.*' -prune \
    -o -type f -print \
    -o -type l -print 2> /dev/null | fzf -m | while read item; do
    printf '%q ' "$item"
  done
  echo
}
bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'

# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": " \C-e\C-u$(history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'

EOF
    else
      cat >> $src << "EOF"
# Key bindings
# ------------
# CTRL-T - Paste the selected file path(s) into the command line
fzf-file-widget() {
  local FILES
  local IFS="
"
  FILES=($(
    find * -path '*/\.*' -prune \
    -o -type f -print \
    -o -type l -print 2> /dev/null | fzf -m))
  unset IFS
  FILES=$FILES:q
  LBUFFER="${LBUFFER%% #} $FILES"
  zle redisplay
}
zle     -N   fzf-file-widget
bindkey '^T' fzf-file-widget

# ALT-C - cd into the selected directory
fzf-cd-widget() {
  cd "${$(find * -path '*/\.*' -prune \
          -o -type d -print 2> /dev/null | fzf):-.}"
  zle reset-prompt
}
zle     -N    fzf-cd-widget
bindkey '\ec' fzf-cd-widget

# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
  LBUFFER=$(history | fzf +s | sed "s/ *[0-9]* *//")
  zle redisplay
}
zle     -N   fzf-history-widget
bindkey '^R' fzf-history-widget

EOF
    fi
  fi

  echo "OK"
done

echo
for shell in bash zsh; do
  rc=~/.${shell}rc
  src="source ~/.fzf.${shell}"

  echo "Update $rc:"
  echo "  - $src"
  if [ $(grep -F "$src" $rc | wc -l) -gt 0 ]; then
    echo "    - Not added (already being sourced)"
  else
    echo $src >> $rc
    echo "    - Added"
  fi
  echo
done

cat << EOF
Finished. Reload your .bashrc or .zshrc.
   source ~/.bashrc  # bash
   source ~/.zshrc   # zsh

To uninstall fzf, simply remove the added line.
EOF