2013-10-23 01:26:55 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
cd `dirname $BASH_SOURCE`
|
2013-11-26 10:01:01 +00:00
|
|
|
fzf_base=`pwd`
|
2013-11-19 16:29:36 +00:00
|
|
|
|
|
|
|
# ruby executable
|
|
|
|
echo -n "Checking Ruby executable ... "
|
2013-11-26 10:01:01 +00:00
|
|
|
ruby=`which ruby`
|
2013-11-19 16:29:36 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ruby executable not found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-01-29 02:04:07 +00:00
|
|
|
|
|
|
|
# System ruby is preferred
|
|
|
|
system_ruby=/usr/bin/ruby
|
|
|
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
2014-05-01 19:51:35 +00:00
|
|
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
2014-01-29 02:04:07 +00:00
|
|
|
[ $? -eq 0 ] && ruby=$system_ruby
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "OK ($ruby)"
|
2013-11-19 16:29:36 +00:00
|
|
|
|
|
|
|
# Curses-support
|
|
|
|
echo -n "Checking Curses support ... "
|
2014-05-01 19:51:35 +00:00
|
|
|
"$ruby" -rcurses -e0 2> /dev/null
|
2013-12-25 16:06:46 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "OK"
|
|
|
|
else
|
|
|
|
echo "Not found"
|
|
|
|
echo "Installing 'curses' gem ... "
|
2014-03-13 02:01:35 +00:00
|
|
|
/usr/bin/env gem install curses -v 1.0.0 --user-install
|
2013-12-25 16:06:46 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2014-03-13 02:01:35 +00:00
|
|
|
echo
|
2013-12-25 16:06:46 +00:00
|
|
|
echo "Failed to install 'curses' gem."
|
2014-03-13 02:01:35 +00:00
|
|
|
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
|
|
|
echo "Make sure that base-devel package group is installed."
|
|
|
|
fi
|
2013-12-25 16:06:46 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2013-11-19 16:29:36 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Ruby version
|
|
|
|
echo -n "Checking Ruby version ... "
|
2014-01-29 02:04:07 +00:00
|
|
|
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
2013-11-19 16:29:36 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo ">= 1.9"
|
2014-05-01 19:51:35 +00:00
|
|
|
"$ruby" --disable-gems -rcurses -e0 2> /dev/null
|
2013-12-25 16:39:17 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
|
|
|
else
|
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
|
|
|
fi
|
2013-11-19 16:29:36 +00:00
|
|
|
else
|
|
|
|
echo "< 1.9"
|
2013-11-26 10:01:01 +00:00
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
2013-11-19 16:29:36 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Auto-completion
|
2013-11-26 10:05:20 +00:00
|
|
|
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
|
2013-11-19 16:29:36 +00:00
|
|
|
echo
|
|
|
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
2013-11-26 10:01:01 +00:00
|
|
|
auto_completion=$?
|
2013-11-19 16:29:36 +00:00
|
|
|
|
2013-12-21 15:18:41 +00: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 16:29:36 +00:00
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
2013-11-26 10:01:01 +00:00
|
|
|
echo -n "Generate ~/.fzf.$shell ... "
|
|
|
|
src=~/.fzf.${shell}
|
2013-11-19 16:29:36 +00:00
|
|
|
|
2013-11-26 10:01:01 +00:00
|
|
|
fzf_completion="source $fzf_base/fzf-completion.${shell}"
|
|
|
|
if [ $auto_completion -ne 0 ]; then
|
|
|
|
fzf_completion="# $fzf_completion"
|
2013-11-19 16:29:36 +00:00
|
|
|
fi
|
|
|
|
|
2013-11-26 10:01:01 +00:00
|
|
|
cat > $src << EOF
|
2013-12-21 15:18:41 +00:00
|
|
|
# Setup fzf function
|
|
|
|
# ------------------
|
2013-11-26 10:01:01 +00:00
|
|
|
unalias fzf 2> /dev/null
|
|
|
|
fzf() {
|
|
|
|
$fzf_cmd "\$@"
|
|
|
|
}
|
|
|
|
export -f fzf > /dev/null
|
2013-12-21 15:18:41 +00:00
|
|
|
|
|
|
|
# Auto-completion
|
|
|
|
# ---------------
|
2014-04-01 11:47:56 +00:00
|
|
|
[[ \$- =~ i ]] && $fzf_completion
|
2013-11-26 10:01:01 +00:00
|
|
|
|
|
|
|
EOF
|
2013-12-21 15:18:41 +00:00
|
|
|
|
|
|
|
if [ $key_bindings -eq 0 ]; then
|
|
|
|
if [ $shell = bash ]; then
|
2014-03-27 16:39:20 +00:00
|
|
|
cat >> $src << "EOFZF"
|
2013-12-21 15:18:41 +00:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2014-04-01 08:49:49 +00:00
|
|
|
__fsel() {
|
2013-12-21 15:18:41 +00:00
|
|
|
find * -path '*/\.*' -prune \
|
|
|
|
-o -type f -print \
|
2014-02-07 09:41:05 +00:00
|
|
|
-o -type d -print \
|
2013-12-21 15:18:41 +00:00
|
|
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
|
|
|
printf '%q ' "$item"
|
|
|
|
done
|
|
|
|
echo
|
2014-03-27 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 11:47:56 +00:00
|
|
|
if [[ $- =~ i ]]; then
|
|
|
|
|
2014-03-27 16:39:20 +00:00
|
|
|
__fsel_tmux() {
|
2014-03-31 04:48:53 +00:00
|
|
|
local height
|
2014-03-28 08:15:32 +00:00
|
|
|
height=${FZF_TMUX_HEIGHT:-40%}
|
|
|
|
if [[ $height =~ %$ ]]; then
|
2014-03-31 04:48:53 +00:00
|
|
|
height="-p ${height%\%}"
|
|
|
|
else
|
|
|
|
height="-l $height"
|
2014-03-28 08:15:32 +00:00
|
|
|
fi
|
2014-04-01 11:47:56 +00:00
|
|
|
tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
2013-12-21 15:18:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 05:29:27 +00:00
|
|
|
__fcd() {
|
2014-03-14 08:46:55 +00:00
|
|
|
local dir
|
2014-03-14 08:53:23 +00:00
|
|
|
dir=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir"
|
2014-03-13 05:29:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 16:39:20 +00:00
|
|
|
__use_tmux=0
|
2014-03-28 08:15:32 +00:00
|
|
|
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
2014-03-27 16:39:20 +00:00
|
|
|
|
2014-03-04 09:53:29 +00:00
|
|
|
if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
|
|
|
# Required to refresh the prompt after fzf
|
|
|
|
bind '"\er": redraw-current-line'
|
|
|
|
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
2014-03-27 16:39:20 +00:00
|
|
|
if [ $__use_tmux -eq 1 ]; then
|
|
|
|
bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
|
|
|
|
else
|
|
|
|
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"'
|
|
|
|
fi
|
2014-03-04 09:53:29 +00:00
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
|
|
|
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
2014-03-13 05:29:27 +00:00
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
2014-03-04 09:53:29 +00:00
|
|
|
else
|
|
|
|
bind '"\C-x\C-e": shell-expand-line'
|
|
|
|
bind '"\C-x\C-r": redraw-current-line'
|
|
|
|
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
|
|
|
# - FIXME: Selected items are attached to the end regardless of cursor position
|
2014-03-27 16:39:20 +00:00
|
|
|
if [ $__use_tmux -eq 1 ]; then
|
|
|
|
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
|
|
|
else
|
|
|
|
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r"'
|
|
|
|
fi
|
2014-03-04 09:53:29 +00:00
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
|
|
|
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
2014-03-13 05:29:27 +00:00
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
2014-03-04 09:53:29 +00:00
|
|
|
fi
|
2013-12-21 15:18:41 +00:00
|
|
|
|
2014-03-27 16:39:20 +00:00
|
|
|
unset __use_tmux
|
|
|
|
|
2014-02-14 16:29:16 +00:00
|
|
|
fi
|
2014-03-27 16:39:20 +00:00
|
|
|
EOFZF
|
2013-12-21 15:18:41 +00:00
|
|
|
else
|
2014-03-27 16:39:20 +00:00
|
|
|
cat >> $src << "EOFZF"
|
2013-12-21 15:18:41 +00:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
|
|
|
# CTRL-T - Paste the selected file path(s) into the command line
|
2014-04-01 08:49:49 +00:00
|
|
|
__fsel() {
|
2014-04-01 12:24:13 +00:00
|
|
|
set -o nonomatch
|
2014-03-27 16:39:20 +00:00
|
|
|
find * -path '*/\.*' -prune \
|
2013-12-21 15:18:41 +00:00
|
|
|
-o -type f -print \
|
2014-02-07 09:41:05 +00:00
|
|
|
-o -type d -print \
|
2014-03-27 16:39:20 +00:00
|
|
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
|
|
|
printf '%q ' "$item"
|
|
|
|
done
|
|
|
|
echo
|
2014-04-01 08:49:49 +00:00
|
|
|
}
|
2014-03-27 16:39:20 +00:00
|
|
|
|
2014-04-01 11:55:06 +00:00
|
|
|
if [[ $- =~ i ]]; then
|
|
|
|
|
2014-03-28 08:15:32 +00:00
|
|
|
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
2014-03-27 16:39:20 +00:00
|
|
|
fzf-file-widget() {
|
2014-03-31 04:48:53 +00:00
|
|
|
local height
|
2014-03-28 08:15:32 +00:00
|
|
|
height=${FZF_TMUX_HEIGHT:-40%}
|
|
|
|
if [[ $height =~ %$ ]]; then
|
2014-03-31 04:48:53 +00:00
|
|
|
height="-p ${height%\%}"
|
|
|
|
else
|
|
|
|
height="-l $height"
|
2014-03-28 08:15:32 +00:00
|
|
|
fi
|
2014-04-01 11:49:36 +00:00
|
|
|
tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
2014-03-27 16:39:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
fzf-file-widget() {
|
2014-04-01 08:49:49 +00:00
|
|
|
LBUFFER="${LBUFFER%% #}$(__fsel)"
|
2014-03-27 16:39:20 +00:00
|
|
|
zle redisplay
|
|
|
|
}
|
|
|
|
fi
|
2013-12-21 15:18:41 +00:00
|
|
|
zle -N fzf-file-widget
|
|
|
|
bindkey '^T' fzf-file-widget
|
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
fzf-cd-widget() {
|
2014-04-01 12:24:13 +00:00
|
|
|
cd "${$(set -o nonomatch; find * -path '*/\.*' -prune \
|
2013-12-21 15:18:41 +00:00
|
|
|
-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() {
|
2014-02-26 09:56:44 +00:00
|
|
|
LBUFFER=$(fc -l 1 | fzf +s | sed "s/ *[0-9]* *//")
|
2013-12-21 15:18:41 +00:00
|
|
|
zle redisplay
|
|
|
|
}
|
|
|
|
zle -N fzf-history-widget
|
|
|
|
bindkey '^R' fzf-history-widget
|
|
|
|
|
2014-04-01 11:55:06 +00:00
|
|
|
fi
|
2014-03-27 16:39:20 +00:00
|
|
|
EOFZF
|
2013-12-21 15:18:41 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-11-26 10:01:01 +00:00
|
|
|
echo "OK"
|
|
|
|
done
|
|
|
|
|
2014-05-02 02:27:32 +00:00
|
|
|
# fish
|
|
|
|
has_fish=0
|
|
|
|
if [ -n "$(which fish)" ]; then
|
|
|
|
has_fish=1
|
|
|
|
echo -n "Generate ~/.config/fish/functions/fzf.fish ... "
|
|
|
|
mkdir -p ~/.config/fish/functions
|
|
|
|
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
|
|
|
function fzf
|
|
|
|
$fzf_cmd \$argv
|
|
|
|
end
|
|
|
|
EOFZF
|
|
|
|
echo "ok"
|
|
|
|
|
|
|
|
if [ $key_bindings -eq 0 ]; then
|
|
|
|
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
|
|
|
cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"
|
|
|
|
function fzf_key_bindings
|
|
|
|
function __fzf_select
|
|
|
|
find * -path '*/\.*' -prune \
|
|
|
|
-o -type f -print \
|
|
|
|
-o -type d -print \
|
|
|
|
-o -type l -print 2> /dev/null | fzf -m | while read item
|
|
|
|
echo -n (echo -n "$item" | sed 's/ /\\\\ /g')' '
|
|
|
|
end
|
|
|
|
echo
|
|
|
|
end
|
2013-11-26 10:01:01 +00:00
|
|
|
|
2014-05-02 02:27:32 +00:00
|
|
|
function __fzf_ctrl_t
|
|
|
|
if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]
|
|
|
|
tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"
|
|
|
|
else
|
|
|
|
__fzf_select > $TMPDIR/fzf.result
|
|
|
|
and commandline -i (cat $TMPDIR/fzf.result)
|
2014-05-02 07:35:36 +00:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-02 02:27:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_ctrl_t_tmux
|
|
|
|
__fzf_select > $TMPDIR/fzf.result
|
|
|
|
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result)
|
2014-05-02 07:35:36 +00:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-02 02:27:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_ctrl_r
|
|
|
|
if history | fzf +s +m > $TMPDIR/fzf.result
|
|
|
|
commandline (cat $TMPDIR/fzf.result)
|
|
|
|
else
|
|
|
|
commandline -f repaint
|
|
|
|
end
|
2014-05-02 07:35:36 +00:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-02 02:27:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_alt_c
|
|
|
|
find * -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m > $TMPDIR/fzf.result
|
|
|
|
if [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
|
|
|
cd (cat $TMPDIR/fzf.result)
|
|
|
|
end
|
|
|
|
commandline -f repaint
|
2014-05-02 07:35:36 +00:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-02 02:27:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_tmux_height
|
|
|
|
if set -q FZF_TMUX_HEIGHT
|
|
|
|
set height $FZF_TMUX_HEIGHT
|
|
|
|
else
|
|
|
|
set height 40%
|
|
|
|
end
|
|
|
|
if echo $height | grep -q -E '%$'
|
|
|
|
echo "-p "(echo $height | sed 's/%$//')
|
|
|
|
else
|
|
|
|
echo "-l $height"
|
|
|
|
end
|
|
|
|
set -e height
|
|
|
|
end
|
|
|
|
|
|
|
|
bind \ct '__fzf_ctrl_t'
|
|
|
|
bind \cr '__fzf_ctrl_r'
|
|
|
|
bind \ec '__fzf_alt_c'
|
|
|
|
end
|
|
|
|
EOFZF
|
|
|
|
echo "ok"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
append_line() {
|
|
|
|
echo "Update $2:"
|
|
|
|
echo " - $1"
|
|
|
|
[ -f "$2" ] || touch "$2"
|
|
|
|
line=$(grep -nF "$1" "$2" | sed 's/:.*//')
|
2014-03-30 15:53:35 +00:00
|
|
|
if [ -n "$line" ]; then
|
|
|
|
echo " - Already exists (line #$line)"
|
2013-11-26 10:01:01 +00:00
|
|
|
else
|
2014-05-02 02:27:32 +00:00
|
|
|
echo "$1" >> "$2"
|
2013-11-26 10:01:01 +00:00
|
|
|
echo " - Added"
|
2013-11-19 16:29:36 +00:00
|
|
|
fi
|
|
|
|
echo
|
2014-05-02 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
|
|
|
append_line "source ~/.fzf.${shell}" ~/.${shell}rc
|
2013-11-19 16:29:36 +00:00
|
|
|
done
|
|
|
|
|
2014-05-02 02:27:32 +00:00
|
|
|
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
|
|
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
|
|
|
append_line "fzf_key_bindings" "$bind_file"
|
|
|
|
fi
|
|
|
|
|
2013-11-29 04:42:13 +00:00
|
|
|
cat << EOF
|
2014-05-02 02:27:32 +00:00
|
|
|
Finished. Restart your shell or reload config file.
|
2013-12-25 16:54:29 +00:00
|
|
|
source ~/.bashrc # bash
|
|
|
|
source ~/.zshrc # zsh
|
2014-05-02 02:27:32 +00:00
|
|
|
EOF
|
|
|
|
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
2013-11-29 04:42:13 +00:00
|
|
|
|
2014-05-02 02:27:32 +00:00
|
|
|
To uninstall fzf, simply remove the added lines.
|
2014-03-30 15:53:35 +00:00
|
|
|
|
|
|
|
For more information, see: https://github.com/junegunn/fzf
|
2013-11-29 04:42:13 +00:00
|
|
|
EOF
|
2013-10-23 01:26:55 +00:00
|
|
|
|