Skip to content
Myr

completion.bash

This file

Path

shell/completion.bash

381 lines · 9.8 KiB · Shell

Last changed by

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

History of this file

Take it

The walk stopped after 200 commits: lines older than 8651448 point at it.

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 1–37why, one step back
1# ____ ____
2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/ completion.bash6#7# - $FZF_TMUX (default: 0)8# - $FZF_TMUX_OPTS (default: empty)9# - $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'3637__fzf_comprun() {

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 38why, one step back
38 if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

line 39why, one step back
39 _fzf_comprun "$@"

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 40why, one step back
40 elif [[ -n "$TMUX_PANE" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "$FZF_TMUX_OPTS" ]]; }; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 41–48why, one step back
41 shift
42 fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"43 else44 shift45 fzf "$@"46 fi47}48

[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d

lines 49–55why, one step back
49__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 back
56 [[ "$f" = _fzf_* ]] && continue

[bash-completion] Unexport _fzf_orig_completion_* variablesTomas Janousekbefore adoption75b8cca

line 57why, one step back
57 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 back
58 if [[ "$l" = *" -o nospace "* ]] && [[ ! "$__fzf_nospace_commands" = *" $cmd "* ]]; then
59 __fzf_nospace_commands="$__fzf_nospace_commands $cmd "60 fi61 fi62 done

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 63–142why, one step back
63}
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 --prompt93 --pointer94 --marker95 --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##*#}"

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 143why, one step back
143 if [[ -n "$orig" ]] && type "$orig" > /dev/null 2>&1; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

line 144why, one step back
144 $orig "$@"

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 145why, one step back
145 elif [[ -n "$_fzf_completion_loader" ]]; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 146–149why, one step back
146 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)
147 _completion_loader "$@"148 ret=$?149 # _completion_loader may not have updated completion for the command

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 150why, one step back
150 if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then

[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d

line 151why, one step back
151 __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 152–173why, one step back
152 if [[ "$__fzf_nospace_commands" = *" $orig_cmd "* ]]; then
153 eval "${orig_complete/ -F / -o nospace -F }"154 else155 eval "$orig_complete"156 fi157 fi158 return $ret159 fi160}161162__fzf_generic_path_completion() {163 local cur base dir leftover matches trigger cmd164 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; do

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 174why, one step back
174 if [[ -z "$dir" ]] || [[ -d "$dir" ]]; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 175–176why, one step back
175 leftover=${base/#"$dir"}
176 leftover=${leftover/#\/}

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

lines 177–178why, one step back
177 [[ -z "$dir" ]] && dir='.'
178 [[ "$dir" != "/" ]] && dir="${dir/%\//}"

[shell] Disable CTRL-ZJunegunn Choibefore adoptionb234647

line 179why, one step back
179 matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $2" __fzf_comprun "$4" -q "$leftover" | while read -r item; do

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 180–183why, one step back
180 printf "%q$3 " "$item"
181 done)182 matches=${matches% }183 [[ -z "$3" ]] && [[ "$__fzf_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 184why, one step back
184 if [[ -n "$matches" ]]; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 185–234why, one step back
185 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() {204 # 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 fi224225 local cur selected trigger cmd post226 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}}234

[shell] Disable CTRL-ZJunegunn Choibefore adoptionb234647

line 235why, one step back
235 selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $str_arg" __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | tr '\n' ' ')

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

line 236why, one step back
236 selected=${selected% } # Strip trailing space not to repeat "-o nospace"

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 237why, one step back
237 if [[ -n "$selected" ]]; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 238–319why, one step back
238 COMPREPLY=("$selected")
239 else240 COMPREPLY=("$cur")241 fi242 printf '\e[5n'243 return 0244 else245 _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"246 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() {263 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 fi270271 _fzf_proc_completion "$@"272}273274_fzf_proc_completion() {275 _fzf_complete -m --preview 'echo {}' --preview-window down:3:wrap --min-height 15 -- "$@" < <(276 command ps -ef | sed 1d277 )278}279280_fzf_proc_completion_post() {281 awk '{print $2}'282}283284_fzf_host_completion() {285 _fzf_complete +m -- "$@" < <(286 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 '[*?]') \287 <(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() {294 _fzf_complete -m -- "$@" < <(295 declare -xp | sed 's/=.*//' | sed 's/.* //'296 )297}298299_fzf_alias_completion() {300 _fzf_complete -m -- "$@" < <(301 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 back
320__fzf_orig_completion < <(complete -p $d_cmds $a_cmds 2> /dev/null)

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 321–332why, one step back
321
322if 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}"

always use [[ ... ]] and not [ ... ] in bash completionsa1346054before adoptione53b4bb

line 333why, one step back
333 if [[ -n "$orig" ]]; then

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 334–364why, one step back
334 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"349done350351# Kill completion (supports empty completion trigger)352complete -F _fzf_complete_kill -o default -o bashdefault kill353354unset cmd d_cmds a_cmds355356_fzf_setup_completion() {357 local kind fn cmd358 kind=$1359 fn=_fzf_${1}_completion360 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then361 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."362 return 1363 fi364 shift

[bash-completion] Optimize __fzf_orig_completion_filterTomas Janousekbefore adoptionef2c29d

line 365why, one step back
365 __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)

Add nowrap, nocycle, nohidden for --preview-windowJunegunn Choibefore adoption8651448

lines 366–381why, one step back
366 for cmd in "$@"; do
367 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