completion.bash
This file
Path
shell/completion.bash
381 lines · 9.7 KiB · Shell
Last changed by
[bash-completion] Fix endless loop when completion.bash sourced twiceTomas Janousekbefore adoption7bd99a2
History of this fileTake it
The walk stopped after 200 commits: lines older than b57e6cf point at it.
[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 1–4why, one step back1# ____ ____2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/[fzf-tmux] Add option to start fzf in tmux popup windowJunegunn Choibefore adoptionbf65e8c
line 5why, one step back5# /_/ /___/_/ completion.bash[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 6–7why, one step back6#7# - $FZF_TMUX (default: 0)[fzf-tmux] Add option to start fzf in tmux popup windowJunegunn Choibefore adoptionbf65e8c
line 8why, one step back8# - $FZF_TMUX_OPTS (default: empty)[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 9–36why, one step back9# - $FZF_COMPLETION_TRIGGER (default: '**')10# - $FZF_COMPLETION_OPTS (default: empty)1112if [[ $- =~ i ]]; then1314# To use custom commands instead of find, override _fzf_compgen_{path,dir}15if ! declare -f _fzf_compgen_path > /dev/null; then16 _fzf_compgen_path() {17 echo "$1"18 command find -L "$1" \19 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \20 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'21 }22fi2324if ! declare -f _fzf_compgen_dir > /dev/null; then25 _fzf_compgen_dir() {26 command find -L "$1" \27 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \28 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'29 }30fi3132###########################################################3334# To redraw line after fzf closes (printf '\e[5n')35bind '"\e[0n": redraw-current-line'36[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
lines 37–39why, one step back37__fzf_comprun() {38 if [ "$(type -t _fzf_comprun 2>&1)" = function ]; then39 _fzf_comprun "$@"[fzf-tmux] Fall back to plain fzf when split failedJunegunn Choibefore adoption540bfd7
line 40why, one step back40 elif [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; }; then[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 41why, one step back41 shift[fzf-tmux] Add option to start fzf in tmux popup windowJunegunn Choibefore adoptionbf65e8c
line 42why, one step back42 fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
lines 43–46why, one step back43 else44 shift45 fzf "$@"46 fi[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 47–48why, one step back47}48[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d
lines 49–55why, one step back49__fzf_orig_completion() {50 local l comp f cmd51 while read -r l; do52 if [[ "$l" =~ ^(.*\ -F)\ *([^ ]*).*\ ([^ ]*)$ ]]; then53 comp="${BASH_REMATCH[1]}"54 f="${BASH_REMATCH[2]}"55 cmd="${BASH_REMATCH[3]}"[bash-completion] Fix endless loop when completion.bash sourced twiceTomas Janousekbefore adoption7bd99a2
line 56why, one step back56 [[ "$f" = _fzf_* ]] && continue[bash-completion] Unexport _fzf_orig_completion_* variablesTomas Janousekbefore adoption75b8cca
line 57why, one step back57 printf -v "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}" "%s" "${comp} %s ${cmd} #${f}"[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d
lines 58–62why, one step back58 if [[ "$l" = *" -o nospace "* ]] && [[ ! "$__fzf_nospace_commands" = *" $cmd "* ]]; then59 __fzf_nospace_commands="$__fzf_nospace_commands $cmd "60 fi61 fi62 done[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 63–92why, one step back63}6465_fzf_opts_completion() {66 local cur prev opts67 COMPREPLY=()68 cur="${COMP_WORDS[COMP_CWORD]}"69 prev="${COMP_WORDS[COMP_CWORD-1]}"70 opts="71 -x --extended72 -e --exact73 --algo74 -i +i75 -n --nth76 --with-nth77 -d --delimiter78 +s --no-sort79 --tac80 --tiebreak81 -m --multi82 --no-mouse83 --bind84 --cycle85 --no-hscroll86 --jump-labels87 --height88 --literal89 --reverse90 --margin91 --inline-info92 --promptMake pointer and multi-select marker customizable (#1844)Hiroki Konishibefore adoption2a60edc
lines 93–94why, one step back93 --pointer94 --marker[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 95–150why, one step back95 --header96 --header-lines97 --ansi98 --tabstop99 --color100 --no-bold101 --history102 --history-size103 --preview104 --preview-window105 -q --query106 -1 --select-1107 -0 --exit-0108 -f --filter109 --print-query110 --expect111 --sync"112113 case "${prev}" in114 --tiebreak)115 COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )116 return 0117 ;;118 --color)119 COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )120 return 0121 ;;122 --history)123 COMPREPLY=()124 return 0125 ;;126 esac127128 if [[ "$cur" =~ ^-|\+ ]]; then129 COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )130 return 0131 fi132133 return 0134}135136_fzf_handle_dynamic_completion() {137 local cmd orig_var orig ret orig_cmd orig_complete138 cmd="$1"139 shift140 orig_cmd="$1"141 orig_var="_fzf_orig_completion_$cmd"142 orig="${!orig_var##*#}"143 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then144 $orig "$@"145 elif [ -n "$_fzf_completion_loader" ]; then146 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)147 _completion_loader "$@"148 ret=$?149 # _completion_loader may not have updated completion for the command150 if [ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]; then[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d
line 151why, one step back151 __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 152–162why, one step back152 if [[ "$__fzf_nospace_commands" = *" $orig_cmd "* ]]; then153 eval "${orig_complete/ -F / -o nospace -F }"154 else155 eval "$orig_complete"156 fi157 fi158 return $ret159 fi160}161162__fzf_generic_path_completion() {[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 163why, one step back163 local cur base dir leftover matches trigger cmd[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 164–178why, one step back164 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"165 COMPREPLY=()166 trigger=${FZF_COMPLETION_TRIGGER-'**'}167 cur="${COMP_WORDS[COMP_CWORD]}"168 if [[ "$cur" == *"$trigger" ]]; then169 base=${cur:0:${#cur}-${#trigger}}170 eval "base=$base"171172 [[ $base = *"/"* ]] && dir="$base"173 while true; do174 if [ -z "$dir" ] || [ -d "$dir" ]; then175 leftover=${base/#"$dir"}176 leftover=${leftover/#\/}177 [ -z "$dir" ] && dir='.'178 [ "$dir" != "/" ] && dir="${dir/%\//}"[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 179why, one step back179 matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $2" __fzf_comprun "$4" -q "$leftover" | while read -r item; do[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 180–203why, one step back180 printf "%q$3 " "$item"181 done)182 matches=${matches% }183 [[ -z "$3" ]] && [[ "$__fzf_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "184 if [ -n "$matches" ]; then185 COMPREPLY=( "$matches" )186 else187 COMPREPLY=( "$cur" )188 fi189 printf '\e[5n'190 return 0191 fi192 dir=$(dirname "$dir")193 [[ "$dir" =~ /$ ]] || dir="$dir"/194 done195 else196 shift197 shift198 shift199 _fzf_handle_dynamic_completion "$cmd" "$@"200 fi201}202203_fzf_complete() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
lines 204–224why, one step back204 # Split arguments around --205 local args rest str_arg i sep206 args=("$@")207 sep=208 for i in "${!args[@]}"; do209 if [[ "${args[$i]}" = -- ]]; then210 sep=$i211 break212 fi213 done214 if [[ -n "$sep" ]]; then215 str_arg=216 rest=("${args[@]:$((sep + 1)):${#args[@]}}")217 args=("${args[@]:0:$sep}")218 else219 str_arg=$1220 args=()221 shift222 rest=("$@")223 fi224[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 225why, one step back225 local cur selected trigger cmd post[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 226–234why, one step back226 post="$(caller 0 | awk '{print $2}')_post"227 type -t "$post" > /dev/null 2>&1 || post=cat228229 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"230 trigger=${FZF_COMPLETION_TRIGGER-'**'}231 cur="${COMP_WORDS[COMP_CWORD]}"232 if [[ "$cur" == *"$trigger" ]]; then233 cur=${cur:0:${#cur}-${#trigger}}234Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 235why, one step back235 selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $str_arg" __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | tr '\n' ' ')[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 236–244why, one step back236 selected=${selected% } # Strip trailing space not to repeat "-o nospace"237 if [ -n "$selected" ]; then238 COMPREPLY=("$selected")239 else240 COMPREPLY=("$cur")241 fi242 printf '\e[5n'243 return 0244 elseChange custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 245why, one step back245 _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 246–262why, one step back246 fi247}248249_fzf_path_completion() {250 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"251}252253# Deprecated. No file only completion.254_fzf_file_completion() {255 _fzf_path_completion "$@"256}257258_fzf_dir_completion() {259 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"260}261262_fzf_complete_kill() {[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 263–269why, one step back263 local trigger=${FZF_COMPLETION_TRIGGER-'**'}264 local cur="${COMP_WORDS[COMP_CWORD]}"265 if [[ -z "$cur" ]]; then266 COMP_WORDS[$COMP_CWORD]=$trigger267 elif [[ "$cur" != *"$trigger" ]]; then268 return 1269 fi[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
line 270why, one step back270[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 271–272why, one step back271 _fzf_proc_completion "$@"272}[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
line 273why, one step back273[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 274–277why, one step back274_fzf_proc_completion() {275 _fzf_complete -m --preview 'echo {}' --preview-window down:3:wrap --min-height 15 -- "$@" < <(276 command ps -ef | sed 1d277 )[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 278–279why, one step back278}279[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 280–283why, one step back280_fzf_proc_completion_post() {281 awk '{print $2}'282}283[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
line 284why, one step back284_fzf_host_completion() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 285why, one step back285 _fzf_complete +m -- "$@" < <([completion] Make host completion handle source files without EOLElvan Owenbefore adoption2986e64
line 286why, one step back286 command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?]') \[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 287–293why, one step back287 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \288 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |289 awk '{if (length($2) > 0) {print $2}}' | sort -u290 )291}292293_fzf_var_completion() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 294why, one step back294 _fzf_complete -m -- "$@" < <([vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 295–299why, one step back295 declare -xp | sed 's/=.*//' | sed 's/.* //'296 )297}298299_fzf_alias_completion() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 300why, one step back300 _fzf_complete -m -- "$@" < <([vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 301–319why, one step back301 alias | sed 's/=.*//' | sed 's/.* //'302 )303}304305# fzf options306complete -o default -F _fzf_opts_completion fzf307308d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"309a_cmds="310 awk cat diff diff3311 emacs emacsclient ex file ftp g++ gcc gvim head hg java312 javac ld less more mvim nvim patch perl python ruby313 sed sftp sort source tail tee uniq vi view vim wc xdg-open314 basename bunzip2 bzip2 chmod chown curl cp dirname du315 find git grep gunzip gzip hg jar316 ln ls mv open rm rsync scp317 svn tar unzip zip"318319# Preserve existing completion[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d
line 320why, one step back320__fzf_orig_completion < <(complete -p $d_cmds $a_cmds 2> /dev/null)[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 321–350why, one step back321322if type _completion_loader > /dev/null 2>&1; then323 _fzf_completion_loader=1324fi325326__fzf_defc() {327 local cmd func opts orig_var orig def328 cmd="$1"329 func="$2"330 opts="$3"331 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"332 orig="${!orig_var}"333 if [ -n "$orig" ]; then334 printf -v def "$orig" "$func"335 eval "$def"336 else337 complete -F "$func" $opts "$cmd"338 fi339}340341# Anything342for cmd in $a_cmds; do343 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"344done345346# Directory347for cmd in $d_cmds; do348 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"349done350[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
line 351why, one step back351# Kill completion (supports empty completion trigger)[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 352–353why, one step back352complete -F _fzf_complete_kill -o default -o bashdefault kill353Remove dead codeJunegunn Choibefore adoption6e464eb
line 354why, one step back354unset cmd d_cmds a_cmds[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 355–360why, one step back355356_fzf_setup_completion() {357 local kind fn cmd358 kind=$1359 fn=_fzf_${1}_completion360 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
line 361why, one step back361 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 362–364why, one step back362 return 1363 fi364 shift[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d
line 365why, one step back365 __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)[vim] Pick up fzf-tmux on $PATH when bin/fzf-tmux is not foundJunegunn Choibefore adoptionb57e6cf
lines 366–381why, one step back366 for cmd in "$@"; do367 case "$kind" in368 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;369 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;370 alias) __fzf_defc "$cmd" "$fn" "-a" ;;371 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;372 esac373 done374}375376# Environment variables / Aliases / Hosts377_fzf_setup_completion 'var' export unset378_fzf_setup_completion 'alias' unalias379_fzf_setup_completion 'host' ssh telnet380381fi