2013-10-22 21:26:55 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
cd `dirname $BASH_SOURCE`
|
2013-11-26 05:01:01 -05:00
|
|
|
fzf_base=`pwd`
|
2013-11-19 11:29:36 -05:00
|
|
|
|
|
|
|
# ruby executable
|
|
|
|
echo -n "Checking Ruby executable ... "
|
2013-11-26 05:01:01 -05:00
|
|
|
ruby=`which ruby`
|
2013-11-19 11:29:36 -05:00
|
|
|
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"
|
2013-12-25 11:06:46 -05:00
|
|
|
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
|
2013-11-19 11:29:36 -05:00
|
|
|
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"
|
2013-12-25 11:39:17 -05:00
|
|
|
/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
|
2013-11-19 11:29:36 -05:00
|
|
|
else
|
|
|
|
echo "< 1.9"
|
2013-11-26 05:01:01 -05:00
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Auto-completion
|
2013-11-26 05:05:20 -05:00
|
|
|
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
|
2013-11-19 11:29:36 -05:00
|
|
|
echo
|
|
|
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
2013-11-26 05:01:01 -05:00
|
|
|
auto_completion=$?
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2013-12-21 10:18:41 -05:00
|
|
|
# Key-bindings
|
|
|
|
read -p "Do you want to add key bindings? ([y]/n) " -n 1 -r
|
|
|
|
echo
|
|
|
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
|
|
|
key_bindings=$?
|
|
|
|
|
2013-11-19 11:29:36 -05:00
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
2013-11-26 05:01:01 -05:00
|
|
|
echo -n "Generate ~/.fzf.$shell ... "
|
|
|
|
src=~/.fzf.${shell}
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2013-11-26 05:01:01 -05:00
|
|
|
fzf_completion="source $fzf_base/fzf-completion.${shell}"
|
|
|
|
if [ $auto_completion -ne 0 ]; then
|
|
|
|
fzf_completion="# $fzf_completion"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
|
2013-11-26 05:01:01 -05:00
|
|
|
cat > $src << EOF
|
2013-12-21 10:18:41 -05:00
|
|
|
# Setup fzf function
|
|
|
|
# ------------------
|
2013-11-26 05:01:01 -05:00
|
|
|
unalias fzf 2> /dev/null
|
|
|
|
fzf() {
|
|
|
|
$fzf_cmd "\$@"
|
|
|
|
}
|
|
|
|
export -f fzf > /dev/null
|
2013-12-21 10:18:41 -05:00
|
|
|
|
|
|
|
# Auto-completion
|
|
|
|
# ---------------
|
2013-11-26 05:01:01 -05:00
|
|
|
$fzf_completion
|
|
|
|
|
|
|
|
EOF
|
2013-12-21 10:18:41 -05:00
|
|
|
|
|
|
|
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
|
2014-01-14 02:51:52 -05:00
|
|
|
__fsel() {
|
2013-12-21 10:18:41 -05:00
|
|
|
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
|
|
|
|
}
|
2014-01-14 02:51:52 -05:00
|
|
|
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"'
|
2013-12-21 10:18:41 -05:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2013-11-26 05:01:01 -05:00
|
|
|
echo "OK"
|
|
|
|
done
|
|
|
|
|
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
|
|
|
rc=~/.${shell}rc
|
|
|
|
src="source ~/.fzf.${shell}"
|
|
|
|
|
|
|
|
echo "Update $rc:"
|
|
|
|
echo " - $src"
|
2013-11-26 05:05:20 -05:00
|
|
|
if [ $(grep -F "$src" $rc | wc -l) -gt 0 ]; then
|
2013-11-26 05:01:01 -05:00
|
|
|
echo " - Not added (already being sourced)"
|
|
|
|
else
|
|
|
|
echo $src >> $rc
|
|
|
|
echo " - Added"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
echo
|
|
|
|
done
|
|
|
|
|
2013-11-28 23:42:13 -05:00
|
|
|
cat << EOF
|
2013-12-25 11:06:46 -05:00
|
|
|
Finished. Reload your .bashrc or .zshrc.
|
2013-12-25 11:54:29 -05:00
|
|
|
source ~/.bashrc # bash
|
|
|
|
source ~/.zshrc # zsh
|
2013-11-28 23:42:13 -05:00
|
|
|
|
|
|
|
To uninstall fzf, simply remove the added line.
|
|
|
|
EOF
|
2013-10-22 21:26:55 -04:00
|
|
|
|