completion.bash
This file
Path
shell/completion.bash
259 lines · 6.5 KiB · Shell
Last changed by
Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
History of this fileTake it
Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 1–7why, one step back1#!/bin/bash2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#FZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
lines 8–9why, one step back8# - $FZF_TMUX (default: 1)9# - $FZF_TMUX_HEIGHT (default: '40%')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 10–18why, one step back10# - $FZF_COMPLETION_TRIGGER (default: '**')11# - $FZF_COMPLETION_OPTS (default: empty)1213_fzf_orig_completion_filter() {14 sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |15 sed 's/[^a-z0-9_= ;]/_/g'16}1718_fzf_opts_completion() {Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 19why, one step back19 local cur prev optsMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 20–21why, one step back20 COMPREPLY=()21 cur="${COMP_WORDS[COMP_CWORD]}"Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 22why, one step back22 prev="${COMP_WORDS[COMP_CWORD-1]}"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 23–30why, one step back23 opts="24 -x --extended25 -e --extended-exact26 -i +i27 -n --nth28 -d --delimiter29 +s --no-sort30 --tacPrepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 31why, one step back31 --tiebreakMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 32–37why, one step back32 -m --multi33 --no-mouse34 +c --no-color35 +2 --no-25636 --black37 --reversePrepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
line 38why, one step back38 --no-hscrollMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 39–44why, one step back39 --prompt40 -q --query41 -1 --select-142 -0 --exit-043 -f --filter44 --print-queryPrepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 45–46why, one step back45 --expect46 --toggle-sortMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
line 47why, one step back47 --sync"Prepare for 0.9.9 releaseJunegunn Choibefore adoptiond08542c
lines 48–54why, one step back4849 case "${prev}" in50 --tiebreak)51 COMPREPLY=( $(compgen -W "length begin end index" -- ${cur}) )52 return 053 ;;54 esacMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 55–81why, one step back5556 if [[ ${cur} =~ ^-|\+ ]]; then57 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )58 return 059 fi6061 return 062}6364_fzf_handle_dynamic_completion() {65 local cmd orig ret66 cmd="$1"67 shift6869 orig=$(eval "echo \$_fzf_orig_completion_$cmd")70 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then71 $orig "$@"72 elif [ -n "$_fzf_completion_loader" ]; then73 _completion_loader "$@"74 ret=$?75 eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)76 source $BASH_SOURCE77 return $ret78 fi79}8081_fzf_path_completion() {Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 82why, one step back82 local cur base dir leftover matches trigger cmd fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 83why, one step back83 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 84–98why, one step back84 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')85 COMPREPLY=()86 trigger=${FZF_COMPLETION_TRIGGER:-**}87 cur="${COMP_WORDS[COMP_CWORD]}"88 if [[ ${cur} == *"$trigger" ]]; then89 base=${cur:0:${#cur}-${#trigger}}90 eval base=$base9192 dir="$base"93 while [ 1 ]; do94 if [ -z "$dir" -o -d "$dir" ]; then95 leftover=${base/#"$dir"}96 leftover=${leftover/#\/}97 [ "$dir" = './' ] && dir=''98 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 99why, one step back99 matches=$(find -L "$dir"* $1 2> /dev/null | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; doMerge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 100–122why, one step back100 printf "%q$3 " "$item"101 done)102 matches=${matches% }103 if [ -n "$matches" ]; then104 COMPREPLY=( "$matches" )105 else106 COMPREPLY=( "$cur" )107 fi108 tput rc109 return 0110 fi111 dir=$(dirname "$dir")112 [[ "$dir" =~ /$ ]] || dir="$dir"/113 done114 else115 shift116 shift117 shift118 _fzf_handle_dynamic_completion "$cmd" "$@"119 fi120}121122_fzf_list_completion() {Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 123why, one step back123 local cur selected trigger cmd src fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 124why, one step back124 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 125–132why, one step back125 read -r src126 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')127 trigger=${FZF_COMPLETION_TRIGGER:-**}128 cur="${COMP_WORDS[COMP_CWORD]}"129 if [[ ${cur} == *"$trigger" ]]; then130 cur=${cur:0:${#cur}-${#trigger}}131132 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 133why, one step back133 selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 134–167why, one step back134 selected=${selected% }135 tput rc136137 if [ -n "$selected" ]; then138 COMPREPLY=("$selected")139 return 0140 fi141 else142 shift143 _fzf_handle_dynamic_completion "$cmd" "$@"144 fi145}146147_fzf_all_completion() {148 _fzf_path_completion \149 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \150 "-m" "" "$@"151}152153_fzf_file_completion() {154 _fzf_path_completion \155 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \156 "-m" "" "$@"157}158159_fzf_dir_completion() {160 _fzf_path_completion \161 "-name .git -prune -o -name .svn -prune -o -type d -print" \162 "" "/" "$@"163}164165_fzf_kill_completion() {166 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1167Fuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 168why, one step back168 local selected fzfFZF_TMUX and FZF_TMUX_HEIGHT for fuzzy completionJunegunn Choibefore adoption3ec83ba
line 169why, one step back169 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
line 170why, one step back170 tput scFuzzy completion using fzf-tmuxJunegunn Choibefore adoptiona4f3d09
line 171why, one step back171 selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')Merge pull request #145 from junegunn/refactor-shell-extJunegunn Choibefore adoption5eef0ac
lines 172–259why, one step back172 tput rc173174 if [ -n "$selected" ]; then175 COMPREPLY=( "$selected" )176 return 0177 fi178}179180_fzf_telnet_completion() {181 _fzf_list_completion '+m' "$@" << "EOF"182 \grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u183EOF184}185186_fzf_ssh_completion() {187 _fzf_list_completion '+m' "$@" << "EOF"188 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 -u189EOF190}191192_fzf_env_var_completion() {193 _fzf_list_completion '-m' "$@" << "EOF"194 declare -xp | sed 's/=.*//' | sed 's/.* //'195EOF196}197198_fzf_alias_completion() {199 _fzf_list_completion '-m' "$@" << "EOF"200 alias | sed 's/=.*//' | sed 's/.* //'201EOF202}203204# fzf options205complete -F _fzf_opts_completion fzf206207d_cmds="cd pushd rmdir"208f_cmds="209 awk cat diff diff3210 emacs ex file ftp g++ gcc gvim head hg java211 javac ld less more mvim patch perl python ruby212 sed sftp sort source tail tee uniq vi view vim wc"213a_cmds="214 basename bunzip2 bzip2 chmod chown curl cp dirname du215 find git grep gunzip gzip hg jar216 ln ls mv open rm rsync scp217 svn tar unzip zip"218x_cmds="kill ssh telnet unset unalias export"219220# Preserve existing completion221if [ "$_fzf_completion_loaded" != '0.8.6-1' ]; then222 # Really wish I could use associative array but OSX comes with bash 3.2 :(223 eval $(complete | \grep '\-F' | \grep -v _fzf_ |224 \grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)225 export _fzf_completion_loaded=0.8.6-1226fi227228if type _completion_loader > /dev/null 2>&1; then229 _fzf_completion_loader=1230fi231232# Directory233for cmd in $d_cmds; do234 complete -F _fzf_dir_completion -o nospace -o plusdirs $cmd235done236237# File238for cmd in $f_cmds; do239 complete -F _fzf_file_completion -o default -o bashdefault $cmd240done241242# Anything243for cmd in $a_cmds; do244 complete -F _fzf_all_completion -o default -o bashdefault $cmd245done246247# Kill completion248complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill249250# Host completion251complete -F _fzf_ssh_completion -o default -o bashdefault ssh252complete -F _fzf_telnet_completion -o default -o bashdefault telnet253254# Environment variables / Aliases255complete -F _fzf_env_var_completion -o default -o bashdefault unset256complete -F _fzf_env_var_completion -o default -o bashdefault export257complete -F _fzf_alias_completion -o default -o bashdefault unalias258259unset cmd d_cmds f_cmds a_cmds x_cmds