2014-11-30 15:02:34 -05:00
|
|
|
#!/usr/bin/env bash
|
2013-10-22 21:26:55 -04:00
|
|
|
|
2015-02-25 11:42:15 -05:00
|
|
|
version=0.9.4
|
2015-01-04 12:17:26 -05:00
|
|
|
|
2015-01-04 22:21:26 -05:00
|
|
|
cd $(dirname $BASH_SOURCE)
|
|
|
|
fzf_base=$(pwd)
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2015-01-04 00:29:42 -05:00
|
|
|
ask() {
|
|
|
|
read -p "$1 ([y]/n) " -n 1 -r
|
|
|
|
echo
|
|
|
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
|
|
|
}
|
|
|
|
|
2015-01-04 22:21:26 -05:00
|
|
|
check_binary() {
|
2015-01-07 06:08:05 -05:00
|
|
|
echo -n " - Checking fzf executable ... "
|
2015-01-13 09:46:38 -05:00
|
|
|
local output=$("$fzf_base"/bin/fzf --version 2>&1)
|
|
|
|
if [ "$version" = "$output" ]; then
|
|
|
|
echo "$output"
|
2015-01-17 06:40:00 -05:00
|
|
|
binary_error=""
|
2015-01-13 09:46:38 -05:00
|
|
|
else
|
|
|
|
echo "$output != $version"
|
2015-01-07 06:08:05 -05:00
|
|
|
rm -f "$fzf_base"/bin/fzf
|
2015-01-13 09:46:38 -05:00
|
|
|
binary_error="Invalid binary"
|
|
|
|
return 1
|
2015-01-04 22:21:26 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-01-07 06:08:05 -05:00
|
|
|
symlink() {
|
|
|
|
echo " - Creating symlink: bin/$1 -> bin/fzf"
|
2015-01-12 22:29:12 -05:00
|
|
|
(cd "$fzf_base"/bin &&
|
2015-01-17 06:40:00 -05:00
|
|
|
rm -f fzf &&
|
2015-01-12 22:29:12 -05:00
|
|
|
ln -sf $1 fzf)
|
2015-01-17 06:40:00 -05:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
binary_error="Failed to create symlink"
|
|
|
|
return 1
|
|
|
|
fi
|
2015-01-07 06:08:05 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 14:49:30 -05:00
|
|
|
download() {
|
2015-01-13 09:46:38 -05:00
|
|
|
echo "Downloading bin/fzf ..."
|
2015-01-14 16:06:22 -05:00
|
|
|
if [[ ! $1 =~ dev && -x "$fzf_base"/bin/fzf ]]; then
|
2015-01-07 06:08:05 -05:00
|
|
|
echo " - Already exists"
|
2015-01-13 09:46:38 -05:00
|
|
|
check_binary && return
|
|
|
|
elif [ -x "$fzf_base"/bin/$1 ]; then
|
2015-01-17 06:40:00 -05:00
|
|
|
symlink $1 && check_binary && return
|
2015-01-04 00:29:42 -05:00
|
|
|
fi
|
2015-01-03 12:42:58 -05:00
|
|
|
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
|
|
|
if [ $? -ne 0 ]; then
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_error="Failed to create bin directory"
|
|
|
|
return
|
2015-01-03 12:42:58 -05:00
|
|
|
fi
|
|
|
|
|
2015-01-06 10:24:05 -05:00
|
|
|
local url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
|
2015-01-03 12:42:58 -05:00
|
|
|
if which curl > /dev/null; then
|
|
|
|
curl -fL $url | tar -xz
|
|
|
|
elif which wget > /dev/null; then
|
|
|
|
wget -O - $url | tar -xz
|
2014-06-20 12:43:25 -04:00
|
|
|
else
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_error="curl or wget not found"
|
|
|
|
return
|
2015-01-03 12:42:58 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f $1 ]; then
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_error="Failed to download ${1}"
|
|
|
|
return
|
2014-06-20 12:43:25 -04:00
|
|
|
fi
|
2015-01-03 12:42:58 -05:00
|
|
|
|
2015-01-07 06:08:05 -05:00
|
|
|
chmod +x $1 && symlink $1 && check_binary
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 12:42:58 -05:00
|
|
|
# Try to download binary executable
|
2015-01-04 11:25:54 -05:00
|
|
|
archi=$(uname -sm)
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_available=1
|
|
|
|
binary_error=""
|
2015-01-05 12:04:06 -05:00
|
|
|
case "$archi" in
|
2015-01-06 10:24:05 -05:00
|
|
|
Darwin\ x86_64) download fzf-$version-darwin_amd64 ;;
|
|
|
|
Darwin\ i*86) download fzf-$version-darwin_386 ;;
|
|
|
|
Linux\ x86_64) download fzf-$version-linux_amd64 ;;
|
|
|
|
Linux\ i*86) download fzf-$version-linux_386 ;;
|
|
|
|
*) binary_available=0 ;;
|
2015-01-05 12:04:06 -05:00
|
|
|
esac
|
2015-01-03 12:42:58 -05:00
|
|
|
|
2015-01-04 22:21:26 -05:00
|
|
|
cd "$fzf_base"
|
|
|
|
if [ -n "$binary_error" ]; then
|
2015-01-03 12:42:58 -05:00
|
|
|
if [ $binary_available -eq 0 ]; then
|
2015-01-04 22:21:26 -05:00
|
|
|
echo "No prebuilt binary for $archi ... "
|
2015-01-03 12:42:58 -05:00
|
|
|
else
|
2015-01-07 06:08:05 -05:00
|
|
|
echo " - $binary_error !!!"
|
2015-01-03 12:42:58 -05:00
|
|
|
fi
|
|
|
|
echo "Installing legacy Ruby version ..."
|
2015-01-01 14:49:30 -05:00
|
|
|
|
|
|
|
# ruby executable
|
|
|
|
echo -n "Checking Ruby executable ... "
|
|
|
|
ruby=`which ruby`
|
2013-12-25 11:06:46 -05:00
|
|
|
if [ $? -ne 0 ]; then
|
2015-01-07 06:08:05 -05:00
|
|
|
echo "ruby executable not found !!!"
|
2013-12-25 11:06:46 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2015-01-01 14:49:30 -05:00
|
|
|
# System ruby is preferred
|
|
|
|
system_ruby=/usr/bin/ruby
|
|
|
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
|
|
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
|
|
|
[ $? -eq 0 ] && ruby=$system_ruby
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "OK ($ruby)"
|
|
|
|
|
|
|
|
# Curses-support
|
|
|
|
echo -n "Checking Curses support ... "
|
|
|
|
"$ruby" -rcurses -e0 2> /dev/null
|
2013-12-25 11:39:17 -05:00
|
|
|
if [ $? -eq 0 ]; then
|
2015-01-01 14:49:30 -05:00
|
|
|
echo "OK"
|
2013-12-25 11:39:17 -05:00
|
|
|
else
|
2015-01-01 14:49:30 -05:00
|
|
|
echo "Not found"
|
|
|
|
echo "Installing 'curses' gem ... "
|
|
|
|
if (( EUID )); then
|
|
|
|
/usr/bin/env gem install curses --user-install
|
|
|
|
else
|
|
|
|
/usr/bin/env gem install curses
|
|
|
|
fi
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo
|
|
|
|
echo "Failed to install 'curses' gem."
|
|
|
|
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
|
|
|
echo "Make sure that base-devel package group is installed."
|
|
|
|
fi
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Ruby version
|
|
|
|
echo -n "Checking Ruby version ... "
|
|
|
|
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo ">= 1.9"
|
|
|
|
"$ruby" --disable-gems -rcurses -e0 2> /dev/null
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
|
|
|
else
|
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "< 1.9"
|
2013-12-25 11:39:17 -05:00
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
|
|
|
fi
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Auto-completion
|
2015-01-04 00:29:42 -05:00
|
|
|
ask "Do you want to add auto-completion support?"
|
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
|
2015-01-04 00:29:42 -05:00
|
|
|
ask "Do you want to add key bindings?"
|
2013-12-21 10:18:41 -05:00
|
|
|
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
|
|
|
|
2015-02-11 20:13:49 -05:00
|
|
|
fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/fzf-completion.${shell}\""
|
2013-11-26 05:01:01 -05:00
|
|
|
if [ $auto_completion -ne 0 ]; then
|
|
|
|
fzf_completion="# $fzf_completion"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
|
2015-01-12 12:39:00 -05:00
|
|
|
if [ -n "$binary_error" ]; then
|
|
|
|
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
|
2015-01-12 12:39:00 -05:00
|
|
|
fzf() {
|
|
|
|
$fzf_cmd "\$@"
|
|
|
|
}
|
|
|
|
export -f fzf > /dev/null
|
|
|
|
|
|
|
|
# Auto-completion
|
|
|
|
# ---------------
|
|
|
|
$fzf_completion
|
|
|
|
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
cat > $src << EOF
|
|
|
|
# Setup fzf
|
|
|
|
# ---------
|
|
|
|
unalias fzf 2> /dev/null
|
2015-01-01 14:49:30 -05:00
|
|
|
unset fzf 2> /dev/null
|
2015-01-12 12:39:00 -05:00
|
|
|
if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then
|
|
|
|
export PATH="$fzf_base/bin:\$PATH"
|
2015-01-01 14:49:30 -05:00
|
|
|
fi
|
2013-12-21 10:18:41 -05:00
|
|
|
|
|
|
|
# Auto-completion
|
|
|
|
# ---------------
|
2014-05-03 11:49:29 -04:00
|
|
|
$fzf_completion
|
2013-11-26 05:01:01 -05:00
|
|
|
|
|
|
|
EOF
|
2015-01-12 12:39:00 -05:00
|
|
|
fi
|
2013-12-21 10:18:41 -05:00
|
|
|
|
|
|
|
if [ $key_bindings -eq 0 ]; then
|
|
|
|
if [ $shell = bash ]; then
|
2014-03-27 12:39:20 -04:00
|
|
|
cat >> $src << "EOFZF"
|
2013-12-21 10:18:41 -05:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2014-04-01 04:49:49 -04:00
|
|
|
__fsel() {
|
2015-01-24 12:07:24 -05:00
|
|
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
2013-12-21 10:18:41 -05:00
|
|
|
-o -type f -print \
|
2014-02-07 04:41:05 -05:00
|
|
|
-o -type d -print \
|
2015-01-24 12:07:24 -05:00
|
|
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do
|
2013-12-21 10:18:41 -05:00
|
|
|
printf '%q ' "$item"
|
|
|
|
done
|
|
|
|
echo
|
2014-03-27 12:39:20 -04:00
|
|
|
}
|
|
|
|
|
2014-04-01 07:47:56 -04:00
|
|
|
if [[ $- =~ i ]]; then
|
|
|
|
|
2014-03-27 12:39:20 -04:00
|
|
|
__fsel_tmux() {
|
2014-03-31 00:48:53 -04:00
|
|
|
local height
|
2014-03-28 04:15:32 -04:00
|
|
|
height=${FZF_TMUX_HEIGHT:-40%}
|
|
|
|
if [[ $height =~ %$ ]]; then
|
2014-03-31 00:48:53 -04:00
|
|
|
height="-p ${height%\%}"
|
|
|
|
else
|
|
|
|
height="-l $height"
|
2014-03-28 04:15:32 -04:00
|
|
|
fi
|
2014-04-01 07:47:56 -04:00
|
|
|
tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
2013-12-21 10:18:41 -05:00
|
|
|
}
|
|
|
|
|
2014-03-13 01:29:27 -04:00
|
|
|
__fcd() {
|
2014-03-14 04:46:55 -04:00
|
|
|
local dir
|
2015-01-24 12:07:24 -05:00
|
|
|
dir=$(command find -L ${1:-.} \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
|
|
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m) && printf 'cd %q' "$dir"
|
2014-03-13 01:29:27 -04:00
|
|
|
}
|
|
|
|
|
2014-03-27 12:39:20 -04:00
|
|
|
__use_tmux=0
|
2014-03-28 04:15:32 -04:00
|
|
|
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
2014-03-27 12:39:20 -04:00
|
|
|
|
2014-09-14 00:53:53 -04:00
|
|
|
if [ -z "$(set -o | \grep '^vi.*on')" ]; then
|
2014-03-04 04:53:29 -05:00
|
|
|
# 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 12:39:20 -04: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
|
2014-05-03 22:28:34 -04: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 \C-h"'
|
2014-03-27 12:39:20 -04:00
|
|
|
fi
|
2014-03-04 04:53:29 -05:00
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
2015-02-25 11:42:15 -05:00
|
|
|
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
2014-03-13 01:29:27 -04:00
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
2014-03-04 04:53:29 -05: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 12:39:20 -04:00
|
|
|
if [ $__use_tmux -eq 1 ]; then
|
|
|
|
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
|
|
|
else
|
2014-05-03 22:28:34 -04:00
|
|
|
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
2014-03-27 12:39:20 -04:00
|
|
|
fi
|
2014-06-23 11:14:16 -04:00
|
|
|
bind -m vi-command '"\C-t": "i\C-t"'
|
2014-03-04 04:53:29 -05:00
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
2015-02-25 11:42:15 -05:00
|
|
|
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
2014-06-23 11:14:16 -04:00
|
|
|
bind -m vi-command '"\C-r": "i\C-r"'
|
2014-03-13 01:29:27 -04:00
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
2014-06-23 11:14:16 -04:00
|
|
|
bind -m vi-command '"\ec": "i\ec"'
|
2014-03-04 04:53:29 -05:00
|
|
|
fi
|
2013-12-21 10:18:41 -05:00
|
|
|
|
2014-03-27 12:39:20 -04:00
|
|
|
unset __use_tmux
|
|
|
|
|
2014-02-14 11:29:16 -05:00
|
|
|
fi
|
2014-03-27 12:39:20 -04:00
|
|
|
EOFZF
|
2015-01-24 12:07:24 -05:00
|
|
|
else # zsh
|
2014-03-27 12:39:20 -04:00
|
|
|
cat >> $src << "EOFZF"
|
2013-12-21 10:18:41 -05:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
|
|
|
# CTRL-T - Paste the selected file path(s) into the command line
|
2014-04-01 04:49:49 -04:00
|
|
|
__fsel() {
|
2015-01-24 12:07:24 -05:00
|
|
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
2013-12-21 10:18:41 -05:00
|
|
|
-o -type f -print \
|
2014-02-07 04:41:05 -05:00
|
|
|
-o -type d -print \
|
2015-01-24 12:07:24 -05:00
|
|
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do
|
2014-03-27 12:39:20 -04:00
|
|
|
printf '%q ' "$item"
|
|
|
|
done
|
|
|
|
echo
|
2014-04-01 04:49:49 -04:00
|
|
|
}
|
2014-03-27 12:39:20 -04:00
|
|
|
|
2014-04-01 07:55:06 -04:00
|
|
|
if [[ $- =~ i ]]; then
|
|
|
|
|
2014-03-28 04:15:32 -04:00
|
|
|
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
2014-03-27 12:39:20 -04:00
|
|
|
fzf-file-widget() {
|
2014-03-31 00:48:53 -04:00
|
|
|
local height
|
2014-03-28 04:15:32 -04:00
|
|
|
height=${FZF_TMUX_HEIGHT:-40%}
|
|
|
|
if [[ $height =~ %$ ]]; then
|
2014-03-31 00:48:53 -04:00
|
|
|
height="-p ${height%\%}"
|
|
|
|
else
|
|
|
|
height="-l $height"
|
2014-03-28 04:15:32 -04:00
|
|
|
fi
|
2014-04-01 07:49:36 -04:00
|
|
|
tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
2014-03-27 12:39:20 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
fzf-file-widget() {
|
2014-05-28 11:16:07 -04:00
|
|
|
LBUFFER="${LBUFFER}$(__fsel)"
|
2014-03-27 12:39:20 -04:00
|
|
|
zle redisplay
|
|
|
|
}
|
|
|
|
fi
|
2013-12-21 10:18:41 -05:00
|
|
|
zle -N fzf-file-widget
|
|
|
|
bindkey '^T' fzf-file-widget
|
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
fzf-cd-widget() {
|
2015-01-24 12:07:24 -05:00
|
|
|
cd "${$(command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
|
|
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m):-.}"
|
2013-12-21 10:18:41 -05:00
|
|
|
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() {
|
2015-02-25 11:42:15 -05:00
|
|
|
LBUFFER=$(fc -l 1 | fzf +s --tac +m -n2..,.. | sed "s/ *[0-9*]* *//")
|
2013-12-21 10:18:41 -05:00
|
|
|
zle redisplay
|
|
|
|
}
|
|
|
|
zle -N fzf-history-widget
|
|
|
|
bindkey '^R' fzf-history-widget
|
|
|
|
|
2014-04-01 07:55:06 -04:00
|
|
|
fi
|
2014-03-27 12:39:20 -04:00
|
|
|
EOFZF
|
2013-12-21 10:18:41 -05:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-11-26 05:01:01 -05:00
|
|
|
echo "OK"
|
|
|
|
done
|
|
|
|
|
2014-05-01 22:27:32 -04: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
|
2015-01-04 22:21:26 -05:00
|
|
|
if [ -n "$binary_error" ]; then
|
2015-01-04 00:35:13 -05:00
|
|
|
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
2014-05-01 22:27:32 -04:00
|
|
|
function fzf
|
|
|
|
$fzf_cmd \$argv
|
|
|
|
end
|
|
|
|
EOFZF
|
2015-01-04 00:35:13 -05:00
|
|
|
else
|
|
|
|
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
|
|
|
function fzf
|
|
|
|
$fzf_base/bin/fzf \$argv
|
|
|
|
end
|
|
|
|
EOFZF
|
|
|
|
fi
|
2014-05-21 13:24:13 -04:00
|
|
|
echo "OK"
|
2014-05-01 22:27:32 -04:00
|
|
|
|
|
|
|
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
|
2014-07-05 14:31:32 -04:00
|
|
|
# Due to a bug of fish, we cannot use command substitution,
|
|
|
|
# so we use temporary file instead
|
|
|
|
if [ -z "$TMPDIR" ]
|
|
|
|
set -g TMPDIR /tmp
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_list
|
2015-01-24 12:07:24 -05:00
|
|
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
2014-05-01 22:27:32 -04:00
|
|
|
-o -type f -print \
|
|
|
|
-o -type d -print \
|
2015-01-24 12:07:24 -05:00
|
|
|
-o -type l -print 2> /dev/null | sed 1d | cut -b3-
|
2014-07-05 14:31:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_list_dir
|
2015-01-24 12:07:24 -05:00
|
|
|
command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \
|
|
|
|
-prune -o -type d -print 2> /dev/null | sed 1d | cut -b3-
|
2014-07-05 14:31:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_escape
|
|
|
|
while read item
|
|
|
|
echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '
|
2014-05-01 22:27:32 -04:00
|
|
|
end
|
|
|
|
end
|
2013-11-26 05:01:01 -05:00
|
|
|
|
2014-05-01 22:27:32 -04: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
|
2014-07-05 14:31:32 -04:00
|
|
|
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
|
|
|
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
|
|
|
commandline -f repaint
|
2014-05-02 03:35:36 -04:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-01 22:27:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_ctrl_t_tmux
|
2014-07-05 14:31:32 -04:00
|
|
|
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
|
|
|
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape)
|
2014-05-02 03:35:36 -04:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-01 22:27:32 -04:00
|
|
|
end
|
|
|
|
|
2014-12-12 17:54:35 -05:00
|
|
|
function __fzf_reverse
|
|
|
|
if which tac > /dev/null
|
|
|
|
tac $argv
|
|
|
|
else
|
|
|
|
tail -r $argv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-01 22:27:32 -04:00
|
|
|
function __fzf_ctrl_r
|
2015-02-25 11:42:15 -05:00
|
|
|
history | __fzf_reverse | fzf +s --tac +m > $TMPDIR/fzf.result
|
2014-07-05 14:31:32 -04:00
|
|
|
and commandline (cat $TMPDIR/fzf.result)
|
|
|
|
commandline -f repaint
|
2014-05-02 03:35:36 -04:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-01 22:27:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_alt_c
|
2014-07-05 14:31:32 -04:00
|
|
|
# Fish hangs if the command before pipe redirects (2> /dev/null)
|
|
|
|
__fzf_list_dir | fzf +m > $TMPDIR/fzf.result
|
|
|
|
[ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
|
|
|
and cd (cat $TMPDIR/fzf.result)
|
2014-05-01 22:27:32 -04:00
|
|
|
commandline -f repaint
|
2014-05-02 03:35:36 -04:00
|
|
|
rm -f $TMPDIR/fzf.result
|
2014-05-01 22:27:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_tmux_height
|
|
|
|
if set -q FZF_TMUX_HEIGHT
|
|
|
|
set height $FZF_TMUX_HEIGHT
|
|
|
|
else
|
|
|
|
set height 40%
|
|
|
|
end
|
2014-09-14 00:53:53 -04:00
|
|
|
if echo $height | \grep -q -E '%$'
|
2014-05-01 22:27:32 -04:00
|
|
|
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
|
2014-05-21 13:24:13 -04:00
|
|
|
echo "OK"
|
2014-05-01 22:27:32 -04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
append_line() {
|
|
|
|
echo "Update $2:"
|
|
|
|
echo " - $1"
|
|
|
|
[ -f "$2" ] || touch "$2"
|
2014-09-17 23:55:28 -04:00
|
|
|
if [ $# -lt 3 ]; then
|
2014-09-18 06:03:01 -04:00
|
|
|
line=$(\grep -nF "$1" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
2014-09-17 23:55:28 -04:00
|
|
|
else
|
2014-09-18 06:03:01 -04:00
|
|
|
line=$(\grep -nF "$3" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
2014-09-17 23:55:28 -04:00
|
|
|
fi
|
2014-03-30 11:53:35 -04:00
|
|
|
if [ -n "$line" ]; then
|
2014-09-18 06:03:01 -04:00
|
|
|
echo " - Already exists: line #$line"
|
2013-11-26 05:01:01 -05:00
|
|
|
else
|
2014-05-01 22:27:32 -04:00
|
|
|
echo "$1" >> "$2"
|
2014-09-18 06:03:01 -04:00
|
|
|
echo " + Added"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
echo
|
2014-05-01 22:27:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
2014-09-18 00:01:39 -04:00
|
|
|
append_line "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}"
|
2013-11-19 11:29:36 -05:00
|
|
|
done
|
|
|
|
|
2014-05-01 22:27:32 -04: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"
|
2014-07-05 14:31:32 -04:00
|
|
|
|
|
|
|
echo ' * Due to a known bug of fish, you may have issues running fzf on fish.'
|
|
|
|
echo ' * If that happens, try the following:'
|
|
|
|
echo ' - Remove ~/.config/fish/functions/fzf.fish'
|
|
|
|
echo ' - Place fzf executable in a directory included in $PATH'
|
|
|
|
echo
|
2014-05-01 22:27:32 -04:00
|
|
|
fi
|
|
|
|
|
2013-11-28 23:42:13 -05:00
|
|
|
cat << EOF
|
2014-05-01 22:27:32 -04:00
|
|
|
Finished. Restart your shell or reload config file.
|
2013-12-25 11:54:29 -05:00
|
|
|
source ~/.bashrc # bash
|
|
|
|
source ~/.zshrc # zsh
|
2014-05-01 22:27:32 -04:00
|
|
|
EOF
|
|
|
|
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
2013-11-28 23:42:13 -05:00
|
|
|
|
2014-05-20 12:14:21 -04:00
|
|
|
Use uninstall script to remove fzf.
|
2014-03-30 11:53:35 -04:00
|
|
|
|
|
|
|
For more information, see: https://github.com/junegunn/fzf
|
2013-11-28 23:42:13 -05:00
|
|
|
EOF
|
2013-10-22 21:26:55 -04:00
|
|
|
|