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
Provenance
- Commit
5eef0ac- Tree
1c5ae8f- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent 1
dd6138a- Parent 2
3935aa8
9 files changed+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)10+11+_fzf_orig_completion_filter() {12+ sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |13+ sed 's/[^a-z0-9_= ;]/_/g'14+}15+16+_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"41+42+ if [[ ${cur} =~ ^-|\+ ]]; then43+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )44+ return 045+ fi46+47+ return 048+}49+50+_fzf_handle_dynamic_completion() {51+ local cmd orig ret52+ cmd="$1"53+ shift54+55+ 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+}66+67+_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=$base76+77+ 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+}106+107+_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}}115+116+ tput sc117+ selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')118+ selected=${selected% }119+ tput rc120+121+ if [ -n "$selected" ]; then122+ COMPREPLY=("$selected")123+ return 0124+ fi125+ else126+ shift127+ _fzf_handle_dynamic_completion "$cmd" "$@"128+ fi129+}130+131+_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+}136+137+_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+}142+143+_fzf_dir_completion() {144+ _fzf_path_completion \145+ "-name .git -prune -o -name .svn -prune -o -type d -print" \146+ "" "/" "$@"147+}148+149+_fzf_kill_completion() {150+ [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1151+152+ local selected153+ tput sc154+ selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')155+ tput rc156+157+ if [ -n "$selected" ]; then158+ COMPREPLY=( "$selected" )159+ return 0160+ fi161+}162+163+_fzf_telnet_completion() {164+ _fzf_list_completion '+m' "$@" << "EOF"165+ \grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u166+EOF167+}168+169+_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 -u172+EOF173+}174+175+_fzf_env_var_completion() {176+ _fzf_list_completion '-m' "$@" << "EOF"177+ declare -xp | sed 's/=.*//' | sed 's/.* //'178+EOF179+}180+181+_fzf_alias_completion() {182+ _fzf_list_completion '-m' "$@" << "EOF"183+ alias | sed 's/=.*//' | sed 's/.* //'184+EOF185+}186+187+# fzf options188+complete -F _fzf_opts_completion fzf189+190+d_cmds="cd pushd rmdir"191+f_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"196+a_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"201+x_cmds="kill ssh telnet unset unalias export"202+203+# Preserve existing completion204+if [ "$_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-1209+fi210+211+if type _completion_loader > /dev/null 2>&1; then212+ _fzf_completion_loader=1213+fi214+215+# Directory216+for cmd in $d_cmds; do217+ complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd218+done219+220+# File221+for cmd in $f_cmds; do222+ complete -F _fzf_file_completion -o default -o bashdefault $cmd223+done224+225+# Anything226+for cmd in $a_cmds; do227+ complete -F _fzf_all_completion -o default -o bashdefault $cmd228+done229+230+# Kill completion231+complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill232+233+# Host completion234+complete -F _fzf_ssh_completion -o default -o bashdefault ssh235+complete -F _fzf_telnet_completion -o default -o bashdefault telnet236+237+# Environment variables / Aliases238+complete -F _fzf_env_var_completion -o default -o bashdefault unset239+complete -F _fzf_env_var_completion -o default -o bashdefault export240+complete -F _fzf_alias_completion -o default -o bashdefault unalias241+242+unset cmd d_cmds f_cmds a_cmds x_cmdsadded:shell/key-bindings.bash+75 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475# Key bindings# ------------__fsel() { command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ -o -type f -print \ -o -type d -print \ -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do printf '%q ' "$item" done echo}
if [[ $- =~ i ]]; then
__fsel_tmux() { local height height=${FZF_TMUX_HEIGHT:-40%} if [[ $height =~ %$ ]]; then height="-p ${height%\%}" else height="-l $height" fi tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"}
__fcd() { local dir 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"}
__use_tmux=0[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
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 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 \C-h"' fi
# CTRL-R - Paste the selected command from history into the command line bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
# ALT-C - cd into the selected directory bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'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 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\exa "' fi bind -m vi-command '"\C-t": "i\C-t"'
# CTRL-R - Paste the selected command from history into the command line 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"' bind -m vi-command '"\C-r": "i\C-r"'
# ALT-C - cd into the selected directory bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"' bind -m vi-command '"\ec": "i\ec"'fi
unset __use_tmux
fiadded:shell/key-bindings.fish+79 added
1+# Key bindings2+# ------------3+function 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+ end9+10+ 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+ end16+17+ 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+ end21+22+ function __fzf_escape23+ while read item24+ echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '25+ end26+ end27+28+ 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+ end38+39+ 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+ end44+45+ 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+ end51+52+ 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+ end60+61+ 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+ end74+75+ bind \ct '__fzf_ctrl_t'76+ bind \cr '__fzf_ctrl_r'77+ bind \ec '__fzf_alt_c'78+end79+added:shell/key-bindings.zsh+59 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# Key bindings# ------------# CTRL-T - Paste the selected file path(s) into the command line__fsel() { command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ -o -type f -print \ -o -type d -print \ -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do printf '%q ' "$item" done echo}
if [[ $- =~ i ]]; then
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then fzf-file-widget() { local height height=${FZF_TMUX_HEIGHT:-40%} if [[ $height =~ %$ ]]; then height="-p ${height%\%}" else height="-l $height" fi tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'" }else fzf-file-widget() { LBUFFER="${LBUFFER}$(__fsel)" zle redisplay }fizle -N fzf-file-widgetbindkey '^T' fzf-file-widget
# ALT-C - cd into the selected directoryfzf-cd-widget() { 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):-.}" zle reset-prompt}zle -N fzf-cd-widgetbindkey '\ec' fzf-cd-widget
# CTRL-R - Paste the selected command from history into the command linefzf-history-widget() { local selected if selected=$(fc -l 1 | fzf +s --tac +m -n2..,.. -q "$LBUFFER"); then num=$(echo "$selected" | head -1 | awk '{print $1}' | sed 's/[^0-9]//g') LBUFFER=!$num zle expand-history fi zle redisplay}zle -N fzf-history-widgetbindkey '^R' fzf-history-widget
fi
modified:fzf+2 added-1 removed
621 unmodified lines622 623 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 else704 unmodified linesdeleted: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)10-11-_fzf_orig_completion_filter() {12- sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |13- sed 's/[^a-z0-9_= ;]/_/g'14-}15-16-_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"41-42- if [[ ${cur} =~ ^-|\+ ]]; then43- COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )44- return 045- fi46-47- return 048-}49-50-_fzf_handle_dynamic_completion() {51- local cmd orig ret52- cmd="$1"53- shift54-55- 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-}66-67-_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=$base76-77- 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-}106-107-_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}}115-116- tput sc117- selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')118- selected=${selected% }119- tput rc120-121- if [ -n "$selected" ]; then122- COMPREPLY=("$selected")123- return 0124- fi125- else126- shift127- _fzf_handle_dynamic_completion "$cmd" "$@"128- fi129-}130-131-_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-}136-137-_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-}142-143-_fzf_dir_completion() {144- _fzf_path_completion \145- "-name .git -prune -o -name .svn -prune -o -type d -print" \146- "" "/" "$@"147-}148-149-_fzf_kill_completion() {150- [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1151-152- local selected153- tput sc154- selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')155- tput rc156-157- if [ -n "$selected" ]; then158- COMPREPLY=( "$selected" )159- return 0160- fi161-}162-163-_fzf_telnet_completion() {164- _fzf_list_completion '+m' "$@" << "EOF"165- \grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u166-EOF167-}168-169-_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 -u172-EOF173-}174-175-_fzf_env_var_completion() {176- _fzf_list_completion '-m' "$@" << "EOF"177- declare -xp | sed 's/=.*//' | sed 's/.* //'178-EOF179-}180-181-_fzf_alias_completion() {182- _fzf_list_completion '-m' "$@" << "EOF"183- alias | sed 's/=.*//' | sed 's/.* //'184-EOF185-}186-187-# fzf options188-complete -F _fzf_opts_completion fzf189-190-d_cmds="cd pushd rmdir"191-f_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"196-a_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"201-x_cmds="kill ssh telnet unset unalias export"202-203-# Preserve existing completion204-if [ "$_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-1209-fi210-211-if type _completion_loader > /dev/null 2>&1; then212- _fzf_completion_loader=1213-fi214-215-# Directory216-for cmd in $d_cmds; do217- complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd218-done219-220-# File221-for cmd in $f_cmds; do222- complete -F _fzf_file_completion -o default -o bashdefault $cmd223-done224-225-# Anything226-for cmd in $a_cmds; do227- complete -F _fzf_all_completion -o default -o bashdefault $cmd228-done229-230-# Kill completion231-complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill232-233-# Host completion234-complete -F _fzf_ssh_completion -o default -o bashdefault ssh235-complete -F _fzf_telnet_completion -o default -o bashdefault telnet236-237-# Environment variables / Aliases238-complete -F _fzf_env_var_completion -o default -o bashdefault unset239-complete -F _fzf_env_var_completion -o default -o bashdefault export240-complete -F _fzf_alias_completion -o default -o bashdefault unalias241-242-unset cmd d_cmds f_cmds a_cmds x_cmdsdeleted:fzf-completion.zsh-9 removed
123456789#!/bin/zsh# ____ ____# / __/___ / __/# / /_/_ / / /_# / __/ / /_/ __/# /_/ /___/_/-completion.zsh#
# TODOmodified:install+31 added-275 removed
142 unmodified lines143 else144 echo "< 1.9"145 fzf_cmd="$ruby $fzf_base/fzf"146 fi147+148+ # 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"155 fi156 157 # Auto-completion158 ask "Do you want to add auto-completion support?"7 unmodified lines166 for shell in bash zsh; do167 echo -n "Generate ~/.fzf.$shell ... "168 src=~/.fzf.${shell}169 162- 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 fi174 167- if [ -n "$binary_error" ]; then168- cat > $src << EOF169-# Setup fzf function170-# ------------------171-unalias fzf 2> /dev/null172-fzf() {173- $fzf_cmd "\$@"174-}175-export -f fzf > /dev/null176-if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then177- export PATH="$fzf_base/bin:\$PATH"178-fi175+ fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""176+ if [ $key_bindings -ne 0 ]; then177+ fzf_key_bindings="# $fzf_key_bindings"178+ fi179 180-# Auto-completion181-# ---------------182-$fzf_completion183-184-EOF185- else186- cat > $src << EOF180+ cat > $src << EOF181 # Setup fzf182 # ---------189-unalias fzf 2> /dev/null190-unset fzf 2> /dev/null183 if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then184 export PATH="$fzf_base/bin:\$PATH"185 fi186 187 # Auto-completion188 # ---------------189 $fzf_completion190 199-EOF200- fi201-202- 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_bindings194 217-if [[ $- =~ i ]]; then218-219-__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-}229-230-__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-}235-236-__use_tmux=0237-[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1238-239-if [ -z "$(set -o | \grep '^vi.*on')" ]; then240- # Required to refresh the prompt after fzf241- bind '"\er": redraw-current-line'242-243- # 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- fi249-250- # 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"'252-253- # ALT-C - cd into the selected directory254- bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'255-else256- bind '"\C-x\C-e": shell-expand-line'257- bind '"\C-x\C-r": redraw-current-line'258-259- # 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"'267-268- # 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"'271-272- # 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"'275-fi276-277-unset __use_tmux278-279-fi280-EOFZF195+EOF281- 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-}295-296-if [[ $- =~ i ]]; then297-298-if [ -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- }309-else310- fzf-file-widget() {311- LBUFFER="${LBUFFER}$(__fsel)"312- zle redisplay313- }314-fi315-zle -N fzf-file-widget316-bindkey '^T' fzf-file-widget317-318-# ALT-C - cd into the selected directory319-fzf-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-}324-zle -N fzf-cd-widget325-bindkey '\ec' fzf-cd-widget326-327-# CTRL-R - Paste the selected command from history into the command line328-fzf-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-}337-zle -N fzf-history-widget338-bindkey '^R' fzf-history-widget339-340-fi341-EOFZF342- fi343- fi344-196 echo "OK"197 done198 199 # fish200 has_fish=0350-if [ -n "$(which fish)" ]; then201+if [ -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/bin207+EOF208+ [ $? -eq 0 ] && echo "OK" || echo "Failed"209+210 mkdir -p ~/.config/fish/functions354- if [ -n "$binary_error" ]; then355- cat > ~/.config/fish/functions/fzf.fish << EOFZF356-function fzf357- $fzf_cmd \$argv358-end359-EOFZF360- else361- cat > ~/.config/fish/functions/fzf.fish << EOFZF362-function fzf363- $fzf_base/bin/fzf \$argv364-end365-EOFZF211+ 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"215 216 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"372-function 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- end378-379- 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- end385-386- 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- end390-391- function __fzf_escape392- while read item393- echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '394- end395- end396-397- 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- end407-408- 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- end413-414- function __fzf_reverse415- if which tac > /dev/null416- tac $argv417- else418- tail -r $argv419- end420- end421-422- 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- end428-429- 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- end437-438- 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- end451-452- bind \ct '__fzf_ctrl_t'453- bind \cr '__fzf_ctrl_r'454- bind \ec '__fzf_alt_c'455-end456-EOFZF457- 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 fi221 fi222 223 append_line() {21 unmodified lines245 246 if [ $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"487-488- 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- echo249 fi250 251 cat << EOF252 Finished. Restart your shell or reload config file.10 unmodified linesmodified:uninstall+1 added
66 unmodified lines67 fi68 69 if [ -d ~/.config/fish/functions ]; then70 remove ~/.config/fish/functions/fzf.fish71+ remove ~/.config/fish/functions/fzf_key_bindings.fish72 73 if [ "$(ls -A ~/.config/fish/functions)" ]; then74 echo "Can't delete non-empty directory: \"~/.config/fish/functions\""75 else4 unmodified lines