completion.bash
This file
Path
shell/completion.bash
382 lines · 10.1 KiB · Shell
Last changed by
[shell] Update kill completionJunegunn Choibefore adoption302e21f
History of this fileTake it
The walk stopped after 200 commits: lines older than 3d2376a point at it.
Add color name 'preview-label' (#3053)Junegunn Choibefore adoption3d2376a
lines 1–272why, one step back1# ____ ____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 back273 _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 BusyBoxAdd color name 'preview-label' (#3053)Junegunn Choibefore adoption3d2376a
lines 276–312why, one step back276 )277}278279_fzf_proc_completion_post() {280 awk '{print $2}'281}282283_fzf_host_completion() {284 _fzf_complete +m -- "$@" < <(285 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 '[*?%]') \286 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \287 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |288 awk '{if (length($2) > 0) {print $2}}' | sort -u289 )290}291292_fzf_var_completion() {293 _fzf_complete -m -- "$@" < <(294 declare -xp | sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'295 )296}297298_fzf_alias_completion() {299 _fzf_complete -m -- "$@" < <(300 alias | sed -En 's|^alias ([^=]+).*|\1|p'301 )302}303304# fzf options305complete -o default -F _fzf_opts_completion fzf306# fzf-tmux is a thin fzf wrapper that has only a few more options than fzf307# itself. As a quick improvement we take fzf's completion. Adding the few extra308# fzf-tmux specific options (like `-w WIDTH`) are left as a future patch.309complete -o default -F _fzf_opts_completion fzf-tmux310311d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"312a_cmds="Add bat to bash autocomplete commands (#3223)Sten Arthur Laanebefore adoptionae745d9
line 313why, one step back313 awk bat cat diff diff3Add Helix editor to bash autocompletion (#3137)Nachum Barcohenbefore adoptione44f64a
line 314why, one step back314 emacs emacsclient ex file ftp g++ gcc gvim head hg hx javaAdd color name 'preview-label' (#3053)Junegunn Choibefore adoption3d2376a
lines 315–376why, one step back315 javac ld less more mvim nvim patch perl python ruby316 sed sftp sort source tail tee uniq vi view vim wc xdg-open317 basename bunzip2 bzip2 chmod chown curl cp dirname du318 find git grep gunzip gzip hg jar319 ln ls mv open rm rsync scp320 svn tar unzip zip"321322# Preserve existing completion323__fzf_orig_completion < <(complete -p $d_cmds $a_cmds 2> /dev/null)324325if type _completion_loader > /dev/null 2>&1; then326 _fzf_completion_loader=1327fi328329__fzf_defc() {330 local cmd func opts orig_var orig def331 cmd="$1"332 func="$2"333 opts="$3"334 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"335 orig="${!orig_var-}"336 if [[ -n "$orig" ]]; then337 printf -v def "$orig" "$func"338 eval "$def"339 else340 complete -F "$func" $opts "$cmd"341 fi342}343344# Anything345for cmd in $a_cmds; do346 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"347done348349# Directory350for cmd in $d_cmds; do351 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"352done353354unset 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 shift365 __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)366 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 / Hosts / Process[bash] Enable environment variable completion for printenvJunegunn Choibefore adoptionf38a7f7
line 377why, one step back377_fzf_setup_completion 'var' export unset printenvAdd color name 'preview-label' (#3053)Junegunn Choibefore adoption3d2376a
lines 378–382why, one step back378_fzf_setup_completion 'alias' unalias379_fzf_setup_completion 'host' ssh telnet380_fzf_setup_completion 'proc' kill381382fi