Skip to content
Myr

Merge pull request #145 from junegunn/refactor-shell-ext

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Refactor shell extensions

authored by Junegunn Choi · committed by Junegunn Choi · parents dd6138a, 3935aa8 · Browse files · 9 files+489 added−527 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+489 added−527 removed

  • other:+489 added−527 removed

9 files+489 added−527 removed

added:shell/completion.bash+242 added
1#!/bin/bash2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#8# - $FZF_COMPLETION_TRIGGER (default: '**')9# - $FZF_COMPLETION_OPTS (default: empty)1011_fzf_orig_completion_filter() {12 sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |13 sed 's/[^a-z0-9_= ;]/_/g'14}1516_fzf_opts_completion() {17 local cur opts18 COMPREPLY=()19 cur="${COMP_WORDS[COMP_CWORD]}"20 opts="21 -x --extended22 -e --extended-exact23 -i +i24 -n --nth25 -d --delimiter26 +s --no-sort27 --tac28 -m --multi29 --no-mouse30 +c --no-color31 +2 --no-25632 --black33 --reverse34 --prompt35 -q --query36 -1 --select-137 -0 --exit-038 -f --filter39 --print-query40 --sync"4142 if [[ ${cur} =~ ^-|\+ ]]; then43 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )44 return 045 fi4647 return 048}4950_fzf_handle_dynamic_completion() {51 local cmd orig ret52 cmd="$1"53 shift5455 orig=$(eval "echo \$_fzf_orig_completion_$cmd")56 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then57 $orig "$@"58 elif [ -n "$_fzf_completion_loader" ]; then59 _completion_loader "$@"60 ret=$?61 eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)62 source $BASH_SOURCE63 return $ret64 fi65}6667_fzf_path_completion() {68 local cur base dir leftover matches trigger cmd69 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')70 COMPREPLY=()71 trigger=${FZF_COMPLETION_TRIGGER:-**}72 cur="${COMP_WORDS[COMP_CWORD]}"73 if [[ ${cur} == *"$trigger" ]]; then74 base=${cur:0:${#cur}-${#trigger}}75 eval base=$base7677 dir="$base"78 while [ 1 ]; do79 if [ -z "$dir" -o -d "$dir" ]; then80 leftover=${base/#"$dir"}81 leftover=${leftover/#\/}82 [ "$dir" = './' ] && dir=''83 tput sc84 matches=$(find -L "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do85 printf "%q$3 " "$item"86 done)87 matches=${matches% }88 if [ -n "$matches" ]; then89 COMPREPLY=( "$matches" )90 else91 COMPREPLY=( "$cur" )92 fi93 tput rc94 return 095 fi96 dir=$(dirname "$dir")97 [[ "$dir" =~ /$ ]] || dir="$dir"/98 done99 else100 shift101 shift102 shift103 _fzf_handle_dynamic_completion "$cmd" "$@"104 fi105}106107_fzf_list_completion() {108 local cur selected trigger cmd src109 read -r src110 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')111 trigger=${FZF_COMPLETION_TRIGGER:-**}112 cur="${COMP_WORDS[COMP_CWORD]}"113 if [[ ${cur} == *"$trigger" ]]; then114 cur=${cur:0:${#cur}-${#trigger}}115116 tput sc117 selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')118 selected=${selected% }119 tput rc120121 if [ -n "$selected" ]; then122 COMPREPLY=("$selected")123 return 0124 fi125 else126 shift127 _fzf_handle_dynamic_completion "$cmd" "$@"128 fi129}130131_fzf_all_completion() {132 _fzf_path_completion \133 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \134 "-m" "" "$@"135}136137_fzf_file_completion() {138 _fzf_path_completion \139 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \140 "-m" "" "$@"141}142143_fzf_dir_completion() {144 _fzf_path_completion \145 "-name .git -prune -o -name .svn -prune -o -type d -print" \146 "" "/" "$@"147}148149_fzf_kill_completion() {150 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1151152 local selected153 tput sc154 selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')155 tput rc156157 if [ -n "$selected" ]; then158 COMPREPLY=( "$selected" )159 return 0160 fi161}162163_fzf_telnet_completion() {164 _fzf_list_completion '+m' "$@" << "EOF"165 \grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u166EOF167}168169_fzf_ssh_completion() {170 _fzf_list_completion '+m' "$@" << "EOF"171 cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i ^host | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts) | awk '{print $2}' | sort -u172EOF173}174175_fzf_env_var_completion() {176 _fzf_list_completion '-m' "$@" << "EOF"177 declare -xp | sed 's/=.*//' | sed 's/.* //'178EOF179}180181_fzf_alias_completion() {182 _fzf_list_completion '-m' "$@" << "EOF"183 alias | sed 's/=.*//' | sed 's/.* //'184EOF185}186187# fzf options188complete -F _fzf_opts_completion fzf189190d_cmds="cd pushd rmdir"191f_cmds="192 awk cat diff diff3193 emacs ex file ftp g++ gcc gvim head hg java194 javac ld less more mvim patch perl python ruby195 sed sftp sort source tail tee uniq vi view vim wc"196a_cmds="197 basename bunzip2 bzip2 chmod chown curl cp dirname du198 find git grep gunzip gzip hg jar199 ln ls mv open rm rsync scp200 svn tar unzip zip"201x_cmds="kill ssh telnet unset unalias export"202203# Preserve existing completion204if [ "$_fzf_completion_loaded" != '0.8.6-1' ]; then205 # Really wish I could use associative array but OSX comes with bash 3.2 :(206 eval $(complete | \grep '\-F' | \grep -v _fzf_ |207 \grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)208 export _fzf_completion_loaded=0.8.6-1209fi210211if type _completion_loader > /dev/null 2>&1; then212 _fzf_completion_loader=1213fi214215# Directory216for cmd in $d_cmds; do217 complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd218done219220# File221for cmd in $f_cmds; do222 complete -F _fzf_file_completion -o default -o bashdefault $cmd223done224225# Anything226for cmd in $a_cmds; do227 complete -F _fzf_all_completion -o default -o bashdefault $cmd228done229230# Kill completion231complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill232233# Host completion234complete -F _fzf_ssh_completion -o default -o bashdefault ssh235complete -F _fzf_telnet_completion -o default -o bashdefault telnet236237# Environment variables / Aliases238complete -F _fzf_env_var_completion -o default -o bashdefault unset239complete -F _fzf_env_var_completion -o default -o bashdefault export240complete -F _fzf_alias_completion -o default -o bashdefault unalias241242unset cmd d_cmds f_cmds a_cmds x_cmds
added:shell/key-bindings.bash+75 added
added:shell/key-bindings.fish+79 added
1# Key bindings2# ------------3function fzf_key_bindings4 # Due to a bug of fish, we cannot use command substitution,5 # so we use temporary file instead6 if [ -z "$TMPDIR" ]7 set -g TMPDIR /tmp8 end910 function __fzf_list11 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \12 -o -type f -print \13 -o -type d -print \14 -o -type l -print 2> /dev/null | sed 1d | cut -b3-15 end1617 function __fzf_list_dir18 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \19 -prune -o -type d -print 2> /dev/null | sed 1d | cut -b3-20 end2122 function __fzf_escape23 while read item24 echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '25 end26 end2728 function __fzf_ctrl_t29 if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]30 tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"31 else32 __fzf_list | fzf -m > $TMPDIR/fzf.result33 and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)34 commandline -f repaint35 rm -f $TMPDIR/fzf.result36 end37 end3839 function __fzf_ctrl_t_tmux40 __fzf_list | fzf -m > $TMPDIR/fzf.result41 and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape)42 rm -f $TMPDIR/fzf.result43 end4445 function __fzf_ctrl_r46 history | fzf +s +m > $TMPDIR/fzf.result47 and commandline (cat $TMPDIR/fzf.result)48 commandline -f repaint49 rm -f $TMPDIR/fzf.result50 end5152 function __fzf_alt_c53 # Fish hangs if the command before pipe redirects (2> /dev/null)54 __fzf_list_dir | fzf +m > $TMPDIR/fzf.result55 [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]56 and cd (cat $TMPDIR/fzf.result)57 commandline -f repaint58 rm -f $TMPDIR/fzf.result59 end6061 function __fzf_tmux_height62 if set -q FZF_TMUX_HEIGHT63 set height $FZF_TMUX_HEIGHT64 else65 set height 40%66 end67 if echo $height | \grep -q -E '%$'68 echo "-p "(echo $height | sed 's/%$//')69 else70 echo "-l $height"71 end72 set -e height73 end7475 bind \ct '__fzf_ctrl_t'76 bind \cr '__fzf_ctrl_r'77 bind \ec '__fzf_alt_c'78end79
added:shell/key-bindings.zsh+59 added
modified:fzf+2 added-1 removed
621 unmodified lines
622623 def start_reader624 stream =625 if @source.tty?626 if default_command = ENV['FZF_DEFAULT_COMMAND']626 default_command = ENV['FZF_DEFAULT_COMMAND']627 if default_command && !default_command.empty?628 IO.popen(default_command)629 elsif !`which find`.empty?630 IO.popen("find * -path '*/\\.*' -prune -o -type f -print -o -type l -print 2> /dev/null")631 else
704 unmodified lines
deleted:fzf-completion.bash-242 removed
1#!/bin/bash2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#8# - $FZF_COMPLETION_TRIGGER (default: '**')9# - $FZF_COMPLETION_OPTS (default: empty)1011_fzf_orig_completion_filter() {12 sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |13 sed 's/[^a-z0-9_= ;]/_/g'14}1516_fzf_opts_completion() {17 local cur opts18 COMPREPLY=()19 cur="${COMP_WORDS[COMP_CWORD]}"20 opts="21 -x --extended22 -e --extended-exact23 -i +i24 -n --nth25 -d --delimiter26 +s --no-sort27 --tac28 -m --multi29 --no-mouse30 +c --no-color31 +2 --no-25632 --black33 --reverse34 --prompt35 -q --query36 -1 --select-137 -0 --exit-038 -f --filter39 --print-query40 --sync"4142 if [[ ${cur} =~ ^-|\+ ]]; then43 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )44 return 045 fi4647 return 048}4950_fzf_handle_dynamic_completion() {51 local cmd orig ret52 cmd="$1"53 shift5455 orig=$(eval "echo \$_fzf_orig_completion_$cmd")56 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then57 $orig "$@"58 elif [ -n "$_fzf_completion_loader" ]; then59 _completion_loader "$@"60 ret=$?61 eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)62 source $BASH_SOURCE63 return $ret64 fi65}6667_fzf_path_completion() {68 local cur base dir leftover matches trigger cmd69 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')70 COMPREPLY=()71 trigger=${FZF_COMPLETION_TRIGGER:-**}72 cur="${COMP_WORDS[COMP_CWORD]}"73 if [[ ${cur} == *"$trigger" ]]; then74 base=${cur:0:${#cur}-${#trigger}}75 eval base=$base7677 dir="$base"78 while [ 1 ]; do79 if [ -z "$dir" -o -d "$dir" ]; then80 leftover=${base/#"$dir"}81 leftover=${leftover/#\/}82 [ "$dir" = './' ] && dir=''83 tput sc84 matches=$(find -L "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do85 printf "%q$3 " "$item"86 done)87 matches=${matches% }88 if [ -n "$matches" ]; then89 COMPREPLY=( "$matches" )90 else91 COMPREPLY=( "$cur" )92 fi93 tput rc94 return 095 fi96 dir=$(dirname "$dir")97 [[ "$dir" =~ /$ ]] || dir="$dir"/98 done99 else100 shift101 shift102 shift103 _fzf_handle_dynamic_completion "$cmd" "$@"104 fi105}106107_fzf_list_completion() {108 local cur selected trigger cmd src109 read -r src110 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')111 trigger=${FZF_COMPLETION_TRIGGER:-**}112 cur="${COMP_WORDS[COMP_CWORD]}"113 if [[ ${cur} == *"$trigger" ]]; then114 cur=${cur:0:${#cur}-${#trigger}}115116 tput sc117 selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')118 selected=${selected% }119 tput rc120121 if [ -n "$selected" ]; then122 COMPREPLY=("$selected")123 return 0124 fi125 else126 shift127 _fzf_handle_dynamic_completion "$cmd" "$@"128 fi129}130131_fzf_all_completion() {132 _fzf_path_completion \133 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \134 "-m" "" "$@"135}136137_fzf_file_completion() {138 _fzf_path_completion \139 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \140 "-m" "" "$@"141}142143_fzf_dir_completion() {144 _fzf_path_completion \145 "-name .git -prune -o -name .svn -prune -o -type d -print" \146 "" "/" "$@"147}148149_fzf_kill_completion() {150 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1151152 local selected153 tput sc154 selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')155 tput rc156157 if [ -n "$selected" ]; then158 COMPREPLY=( "$selected" )159 return 0160 fi161}162163_fzf_telnet_completion() {164 _fzf_list_completion '+m' "$@" << "EOF"165 \grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u166EOF167}168169_fzf_ssh_completion() {170 _fzf_list_completion '+m' "$@" << "EOF"171 cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i ^host | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts) | awk '{print $2}' | sort -u172EOF173}174175_fzf_env_var_completion() {176 _fzf_list_completion '-m' "$@" << "EOF"177 declare -xp | sed 's/=.*//' | sed 's/.* //'178EOF179}180181_fzf_alias_completion() {182 _fzf_list_completion '-m' "$@" << "EOF"183 alias | sed 's/=.*//' | sed 's/.* //'184EOF185}186187# fzf options188complete -F _fzf_opts_completion fzf189190d_cmds="cd pushd rmdir"191f_cmds="192 awk cat diff diff3193 emacs ex file ftp g++ gcc gvim head hg java194 javac ld less more mvim patch perl python ruby195 sed sftp sort source tail tee uniq vi view vim wc"196a_cmds="197 basename bunzip2 bzip2 chmod chown curl cp dirname du198 find git grep gunzip gzip hg jar199 ln ls mv open rm rsync scp200 svn tar unzip zip"201x_cmds="kill ssh telnet unset unalias export"202203# Preserve existing completion204if [ "$_fzf_completion_loaded" != '0.8.6-1' ]; then205 # Really wish I could use associative array but OSX comes with bash 3.2 :(206 eval $(complete | \grep '\-F' | \grep -v _fzf_ |207 \grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)208 export _fzf_completion_loaded=0.8.6-1209fi210211if type _completion_loader > /dev/null 2>&1; then212 _fzf_completion_loader=1213fi214215# Directory216for cmd in $d_cmds; do217 complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd218done219220# File221for cmd in $f_cmds; do222 complete -F _fzf_file_completion -o default -o bashdefault $cmd223done224225# Anything226for cmd in $a_cmds; do227 complete -F _fzf_all_completion -o default -o bashdefault $cmd228done229230# Kill completion231complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill232233# Host completion234complete -F _fzf_ssh_completion -o default -o bashdefault ssh235complete -F _fzf_telnet_completion -o default -o bashdefault telnet236237# Environment variables / Aliases238complete -F _fzf_env_var_completion -o default -o bashdefault unset239complete -F _fzf_env_var_completion -o default -o bashdefault export240complete -F _fzf_alias_completion -o default -o bashdefault unalias241242unset cmd d_cmds f_cmds a_cmds x_cmds
deleted:fzf-completion.zsh-9 removed
modified:install+31 added-275 removed
142 unmodified lines
143 else144 echo "< 1.9"145 fzf_cmd="$ruby $fzf_base/fzf"146 fi147148 # Create fzf script149 echo -n "Creating wrapper script for fzf ... "150 rm -f "$fzf_base"/bin/fzf151 echo "#!/bin/sh" > "$fzf_base"/bin/fzf152 echo "$fzf_cmd \"\$@\"" >> "$fzf_base"/bin/fzf153 chmod +x "$fzf_base"/bin/fzf154 echo "OK"155fi156157# Auto-completion158ask "Do you want to add auto-completion support?"
7 unmodified lines
166for shell in bash zsh; do167 echo -n "Generate ~/.fzf.$shell ... "168 src=~/.fzf.${shell}169162 fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/fzf-completion.${shell}\""163 if [ $auto_completion -ne 0 ]; then170 fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/shell/completion.${shell}\""171 if [ $shell != bash -o $auto_completion -ne 0 ]; then172 fzf_completion="# $fzf_completion"173 fi174167 if [ -n "$binary_error" ]; then168 cat > $src << EOF169# Setup fzf function170# ------------------171unalias fzf 2> /dev/null172fzf() {173 $fzf_cmd "\$@"174}175export -f fzf > /dev/null176if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then177 export PATH="$fzf_base/bin:\$PATH"178fi175 fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""176 if [ $key_bindings -ne 0 ]; then177 fzf_key_bindings="# $fzf_key_bindings"178 fi179180# Auto-completion181# ---------------182$fzf_completion183184EOF185 else186 cat > $src << EOF180 cat > $src << EOF181# Setup fzf182# ---------189unalias fzf 2> /dev/null190unset fzf 2> /dev/null183if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then184 export PATH="$fzf_base/bin:\$PATH"185fi186187# Auto-completion188# ---------------189$fzf_completion190199EOF200 fi201202 if [ $key_bindings -eq 0 ]; then203 if [ $shell = bash ]; then204 cat >> $src << "EOFZF"191# Key bindings192# ------------207__fsel() {208 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \209 -o -type f -print \210 -o -type d -print \211 -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do212 printf '%q ' "$item"213 done214 echo215}193$fzf_key_bindings194217if [[ $- =~ i ]]; then218219__fsel_tmux() {220 local height221 height=${FZF_TMUX_HEIGHT:-40%}222 if [[ $height =~ %$ ]]; then223 height="-p ${height%\%}"224 else225 height="-l $height"226 fi227 tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"228}229230__fcd() {231 local dir232 dir=$(command find -L ${1:-.} \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \233 -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m) && printf 'cd %q' "$dir"234}235236__use_tmux=0237[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1238239if [ -z "$(set -o | \grep '^vi.*on')" ]; then240 # Required to refresh the prompt after fzf241 bind '"\er": redraw-current-line'242243 # CTRL-T - Paste the selected file path into the command line244 if [ $__use_tmux -eq 1 ]; then245 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"'246 else247 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"'248 fi249250 # CTRL-R - Paste the selected command from history into the command line251 bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'252253 # ALT-C - cd into the selected directory254 bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'255else256 bind '"\C-x\C-e": shell-expand-line'257 bind '"\C-x\C-r": redraw-current-line'258259 # CTRL-T - Paste the selected file path into the command line260 # - FIXME: Selected items are attached to the end regardless of cursor position261 if [ $__use_tmux -eq 1 ]; then262 bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'263 else264 bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'265 fi266 bind -m vi-command '"\C-t": "i\C-t"'267268 # CTRL-R - Paste the selected command from history into the command line269 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"'270 bind -m vi-command '"\C-r": "i\C-r"'271272 # ALT-C - cd into the selected directory273 bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'274 bind -m vi-command '"\ec": "i\ec"'275fi276277unset __use_tmux278279fi280EOFZF195EOF281 else # zsh282 cat >> $src << "EOFZF"283# Key bindings284# ------------285# CTRL-T - Paste the selected file path(s) into the command line286__fsel() {287 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \288 -o -type f -print \289 -o -type d -print \290 -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do291 printf '%q ' "$item"292 done293 echo294}295296if [[ $- =~ i ]]; then297298if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then299 fzf-file-widget() {300 local height301 height=${FZF_TMUX_HEIGHT:-40%}302 if [[ $height =~ %$ ]]; then303 height="-p ${height%\%}"304 else305 height="-l $height"306 fi307 tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"308 }309else310 fzf-file-widget() {311 LBUFFER="${LBUFFER}$(__fsel)"312 zle redisplay313 }314fi315zle -N fzf-file-widget316bindkey '^T' fzf-file-widget317318# ALT-C - cd into the selected directory319fzf-cd-widget() {320 cd "${$(command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \321 -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m):-.}"322 zle reset-prompt323}324zle -N fzf-cd-widget325bindkey '\ec' fzf-cd-widget326327# CTRL-R - Paste the selected command from history into the command line328fzf-history-widget() {329 local selected330 if selected=$(fc -l 1 | fzf +s --tac +m -n2..,.. -q "$LBUFFER"); then331 num=$(echo "$selected" | head -1 | awk '{print $1}' | sed 's/[^0-9]//g')332 LBUFFER=!$num333 zle expand-history334 fi335 zle redisplay336}337zle -N fzf-history-widget338bindkey '^R' fzf-history-widget339340fi341EOFZF342 fi343 fi344196 echo "OK"197done198199# fish200has_fish=0350if [ -n "$(which fish)" ]; then201if [ -n "$(which fish 2> /dev/null)" ]; then202 has_fish=1352 echo -n "Generate ~/.config/fish/functions/fzf.fish ... "203 echo -n "Update fish_user_paths ... "204 fish << EOF205 echo \$fish_user_paths | grep $fzf_base/bin > /dev/null206 or set --universal fish_user_paths \$fish_user_paths $fzf_base/bin207EOF208 [ $? -eq 0 ] && echo "OK" || echo "Failed"209210 mkdir -p ~/.config/fish/functions354 if [ -n "$binary_error" ]; then355 cat > ~/.config/fish/functions/fzf.fish << EOFZF356function fzf357 $fzf_cmd \$argv358end359EOFZF360 else361 cat > ~/.config/fish/functions/fzf.fish << EOFZF362function fzf363 $fzf_base/bin/fzf \$argv364end365EOFZF211 if [ -e ~/.config/fish/functions/fzf.fish ]; then212 echo -n "Remove unnecessary ~/.config/fish/functions/fzf.fish ... "213 rm -f ~/.config/fish/functions/fzf.fish && echo "OK" || echo "Failed"214 fi367 echo "OK"215216 if [ $key_bindings -eq 0 ]; then370 echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "371 cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"372function fzf_key_bindings373 # Due to a bug of fish, we cannot use command substitution,374 # so we use temporary file instead375 if [ -z "$TMPDIR" ]376 set -g TMPDIR /tmp377 end378379 function __fzf_list380 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \381 -o -type f -print \382 -o -type d -print \383 -o -type l -print 2> /dev/null | sed 1d | cut -b3-384 end385386 function __fzf_list_dir387 command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \388 -prune -o -type d -print 2> /dev/null | sed 1d | cut -b3-389 end390391 function __fzf_escape392 while read item393 echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '394 end395 end396397 function __fzf_ctrl_t398 if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]399 tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"400 else401 __fzf_list | fzf -m > $TMPDIR/fzf.result402 and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)403 commandline -f repaint404 rm -f $TMPDIR/fzf.result405 end406 end407408 function __fzf_ctrl_t_tmux409 __fzf_list | fzf -m > $TMPDIR/fzf.result410 and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape)411 rm -f $TMPDIR/fzf.result412 end413414 function __fzf_reverse415 if which tac > /dev/null416 tac $argv417 else418 tail -r $argv419 end420 end421422 function __fzf_ctrl_r423 history | __fzf_reverse | fzf +s --tac +m > $TMPDIR/fzf.result424 and commandline (cat $TMPDIR/fzf.result)425 commandline -f repaint426 rm -f $TMPDIR/fzf.result427 end428429 function __fzf_alt_c430 # Fish hangs if the command before pipe redirects (2> /dev/null)431 __fzf_list_dir | fzf +m > $TMPDIR/fzf.result432 [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]433 and cd (cat $TMPDIR/fzf.result)434 commandline -f repaint435 rm -f $TMPDIR/fzf.result436 end437438 function __fzf_tmux_height439 if set -q FZF_TMUX_HEIGHT440 set height $FZF_TMUX_HEIGHT441 else442 set height 40%443 end444 if echo $height | \grep -q -E '%$'445 echo "-p "(echo $height | sed 's/%$//')446 else447 echo "-l $height"448 end449 set -e height450 end451452 bind \ct '__fzf_ctrl_t'453 bind \cr '__fzf_ctrl_r'454 bind \ec '__fzf_alt_c'455end456EOFZF457 echo "OK"217 echo -n "Symlink ~/.config/fish/functions/fzf_key_bindings.fish ... "218 ln -sf $fzf_base/shell/key-bindings.fish \219 ~/.config/fish/functions/fzf_key_bindings.fish && echo "OK" || echo "Failed"220 fi221fi222223append_line() {
21 unmodified lines
245246if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then247 bind_file=~/.config/fish/functions/fish_user_key_bindings.fish248 append_line "fzf_key_bindings" "$bind_file"487488 echo ' * Due to a known bug of fish, you may have issues running fzf on fish.'489 echo ' * If that happens, try the following:'490 echo ' - Remove ~/.config/fish/functions/fzf.fish'491 echo ' - Place fzf executable in a directory included in $PATH'492 echo249fi250251cat << EOF252Finished. Restart your shell or reload config file.
10 unmodified lines
modified:uninstall+1 added
66 unmodified lines
67fi6869if [ -d ~/.config/fish/functions ]; then70 remove ~/.config/fish/functions/fzf.fish71 remove ~/.config/fish/functions/fzf_key_bindings.fish7273 if [ "$(ls -A ~/.config/fish/functions)" ]; then74 echo "Can't delete non-empty directory: \"~/.config/fish/functions\""75 else
4 unmodified lines