Skip to content
Myr

completion.bash

This file

Path

shell/completion.bash

405 lines · 10.8 KiB · Shell

Last changed by

Basic context-aware completion for ssh command (#3424)Timofei Bredovbefore adoptionedfdcc8

History of this file

Take it

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

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 1–272why, 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' 2> /dev/null3637__fzf_comprun() {38 if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then39 _fzf_comprun "$@"40 elif [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; }; then41 shift42 fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"43 else44 shift45 fzf "$@"46 fi47}4849__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]}"56 [[ "$f" = _fzf_* ]] && continue57 printf -v "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}" "%s" "${comp} %s ${cmd} #${f}"58 if [[ "$l" = *" -o nospace "* ]] && [[ ! "${__fzf_nospace_commands-}" = *" $cmd "* ]]; then59 __fzf_nospace_commands="${__fzf_nospace_commands-} $cmd "60 fi61 fi62 done63}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-}"143 orig="${orig##*#}"144 if [[ -n "$orig" ]] && type "$orig" > /dev/null 2>&1; then145 $orig "$@"146 elif [[ -n "${_fzf_completion_loader-}" ]]; then147 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)148 _completion_loader "$@"149 ret=$?150 # _completion_loader may not have updated completion for the command151 if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then152 __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)153 if [[ "${__fzf_nospace_commands-}" = *" $orig_cmd "* ]]; then154 eval "${orig_complete/ -F / -o nospace -F }"155 else156 eval "$orig_complete"157 fi158 fi159 return $ret160 fi161}162163__fzf_generic_path_completion() {164 local cur base dir leftover matches trigger cmd165 cmd="${COMP_WORDS[0]}"166 if [[ $cmd == \\* ]]; then167 cmd="${cmd:1}"168 fi169 cmd="${cmd//[^A-Za-z0-9_=]/_}"170 COMPREPLY=()171 trigger=${FZF_COMPLETION_TRIGGER-'**'}172 cur="${COMP_WORDS[COMP_CWORD]}"173 if [[ "$cur" == *"$trigger" ]]; then174 base=${cur:0:${#cur}-${#trigger}}175 eval "base=$base"176177 dir=178 [[ $base = *"/"* ]] && dir="$base"179 while true; do180 if [[ -z "$dir" ]] || [[ -d "$dir" ]]; then181 leftover=${base/#"$dir"}182 leftover=${leftover/#\/}183 [[ -z "$dir" ]] && dir='.'184 [[ "$dir" != "/" ]] && dir="${dir/%\//}"185 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; do186 printf "%q " "${item%$3}$3"187 done)188 matches=${matches% }189 [[ -z "$3" ]] && [[ "${__fzf_nospace_commands-}" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "190 if [[ -n "$matches" ]]; then191 COMPREPLY=( "$matches" )192 else193 COMPREPLY=( "$cur" )194 fi195 printf '\e[5n'196 return 0197 fi198 dir=$(dirname "$dir")199 [[ "$dir" =~ /$ ]] || dir="$dir"/200 done201 else202 shift203 shift204 shift205 _fzf_handle_dynamic_completion "$cmd" "$@"206 fi207}208209_fzf_complete() {210 # Split arguments around --211 local args rest str_arg i sep212 args=("$@")213 sep=214 for i in "${!args[@]}"; do215 if [[ "${args[$i]}" = -- ]]; then216 sep=$i217 break218 fi219 done220 if [[ -n "$sep" ]]; then221 str_arg=222 rest=("${args[@]:$((sep + 1)):${#args[@]}}")223 args=("${args[@]:0:$sep}")224 else225 str_arg=$1226 args=()227 shift228 rest=("$@")229 fi230231 local cur selected trigger cmd post232 post="$(caller 0 | awk '{print $2}')_post"233 type -t "$post" > /dev/null 2>&1 || post=cat234235 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"236 trigger=${FZF_COMPLETION_TRIGGER-'**'}237 cur="${COMP_WORDS[COMP_CWORD]}"238 if [[ "$cur" == *"$trigger" ]]; then239 cur=${cur:0:${#cur}-${#trigger}}240241 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' ' ')242 selected=${selected% } # Strip trailing space not to repeat "-o nospace"243 if [[ -n "$selected" ]]; then244 COMPREPLY=("$selected")245 else246 COMPREPLY=("$cur")247 fi248 printf '\e[5n'249 return 0250 else251 _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"252 fi253}254255_fzf_path_completion() {256 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"257}258259# Deprecated. No file only completion.260_fzf_file_completion() {261 _fzf_path_completion "$@"262}263264_fzf_dir_completion() {265 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"266}267268_fzf_complete_kill() {269 _fzf_proc_completion "$@"270}271272_fzf_proc_completion() {

[shell] Update kill completionJunegunn Choibefore adoption302e21f

lines 273–275why, one step back
273 _fzf_complete -m --header-lines=1 --preview 'echo {}' --preview-window down:3:wrap --min-height 15 -- "$@" < <(
274 command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||275 command ps -eo user,pid,ppid,time,args # For BusyBox

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 276–282why, one step back
276 )
277}278279_fzf_proc_completion_post() {280 awk '{print $2}'281}282

Basic context-aware completion for ssh command (#3424)Timofei Bredovbefore adoptionedfdcc8

lines 283–289why, one step back
283__fzf_list_hosts() {
284 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 '[*?%]') \285 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \286 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |287 awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | sort -u288}289

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

line 290why, one step back
290_fzf_host_completion() {

Basic context-aware completion for ssh command (#3424)Timofei Bredovbefore adoptionedfdcc8

line 291why, one step back
291 _fzf_complete +m -- "$@" < <(__fzf_list_hosts "")

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 292–293why, one step back
292}
293

Basic context-aware completion for ssh command (#3424)Timofei Bredovbefore adoptionedfdcc8

lines 294–311why, one step back
294# Values for $1 $2 $3 are described here
295# https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html296# > the first argument ($1) is the name of the command whose arguments are being completed,297# > the second argument ($2) is the word being completed,298# > and the third argument ($3) is the word preceding the word being completed on the current command line.299_fzf_complete_ssh() {300 case $3 in301 -i|-F|-E)302 _fzf_path_completion "$@"303 ;;304 *)305 local user=306 [[ "$2" =~ '@' ]] && user="${2%%@*}@"307 _fzf_complete +m -- "$@" < <(__fzf_list_hosts "$user")308 ;;309 esac310}311

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 312–332why, one step back
312_fzf_var_completion() {
313 _fzf_complete -m -- "$@" < <(314 declare -xp | sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'315 )316}317318_fzf_alias_completion() {319 _fzf_complete -m -- "$@" < <(320 alias | sed -En 's|^alias ([^=]+).*|\1|p'321 )322}323324# fzf options325complete -o default -F _fzf_opts_completion fzf326# fzf-tmux is a thin fzf wrapper that has only a few more options than fzf327# itself. As a quick improvement we take fzf's completion. Adding the few extra328# fzf-tmux specific options (like `-w WIDTH`) are left as a future patch.329complete -o default -F _fzf_opts_completion fzf-tmux330331d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"332a_cmds="

Add bat to bash autocomplete commands (#3223)Sten Arthur Laanebefore adoptionae745d9

line 333why, one step back
333 awk bat cat diff diff3

Add Helix editor to bash autocompletion (#3137)Nachum Barcohenbefore adoptione44f64a

line 334why, one step back
334 emacs emacsclient ex file ftp g++ gcc gvim head hg hx java

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 335–373why, one step back
335 javac ld less more mvim nvim patch perl python ruby
336 sed sftp sort source tail tee uniq vi view vim wc xdg-open337 basename bunzip2 bzip2 chmod chown curl cp dirname du338 find git grep gunzip gzip hg jar339 ln ls mv open rm rsync scp340 svn tar unzip zip"341342# Preserve existing completion343__fzf_orig_completion < <(complete -p $d_cmds $a_cmds 2> /dev/null)344345if type _completion_loader > /dev/null 2>&1; then346 _fzf_completion_loader=1347fi348349__fzf_defc() {350 local cmd func opts orig_var orig def351 cmd="$1"352 func="$2"353 opts="$3"354 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"355 orig="${!orig_var-}"356 if [[ -n "$orig" ]]; then357 printf -v def "$orig" "$func"358 eval "$def"359 else360 complete -F "$func" $opts "$cmd"361 fi362}363364# Anything365for cmd in $a_cmds; do366 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"367done368369# Directory370for cmd in $d_cmds; do371 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"372done373

Basic context-aware completion for ssh command (#3424)Timofei Bredovbefore adoptionedfdcc8

lines 374–376why, one step back
374# ssh
375__fzf_defc ssh _fzf_complete_ssh "-o default -o bashdefault"376

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 377–399why, one step back
377unset cmd d_cmds a_cmds
378379_fzf_setup_completion() {380 local kind fn cmd381 kind=$1382 fn=_fzf_${1}_completion383 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then384 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."385 return 1386 fi387 shift388 __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)389 for cmd in "$@"; do390 case "$kind" in391 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;392 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;393 alias) __fzf_defc "$cmd" "$fn" "-a" ;;394 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;395 esac396 done397}398399# Environment variables / Aliases / Hosts / Process

[bash] Enable environment variable completion for printenvJunegunn Choibefore adoptionf38a7f7

line 400why, one step back
400_fzf_setup_completion 'var' export unset printenv

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

line 401why, one step back
401_fzf_setup_completion 'alias' unalias

Basic context-aware completion for ssh command (#3424)Timofei Bredovbefore adoptionedfdcc8

line 402why, one step back
402_fzf_setup_completion 'host' telnet

Add GET endpoints for getting the state of the finderJunegunn Choibefore adoption750b2a6

lines 403–405why, one step back
403_fzf_setup_completion 'proc' kill
404405fi