completion.bash
This file
Path
shell/completion.bash
372 lines · 9.6 KiB · Shell
Last changed by
Remove dead codeJunegunn Choibefore adoption6e464eb
History of this fileTake it
The walk stopped after 200 commits: lines older than 8e34e6f point at it.
[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
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[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
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)[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 9–11why, one step back9# - $FZF_COMPLETION_TRIGGER (default: '**')10# - $FZF_COMPLETION_OPTS (default: empty)11[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5
lines 12–13why, one step back12if [[ $- =~ i ]]; then13[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 14–18why, one step back14# 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" \[completion] Ignore hg repos (#1777)John Purnellbefore adoption357e82e
line 19why, one step back19 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 20–26why, one step back20 -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" \[completion] Ignore hg repos (#1777)John Purnellbefore adoption357e82e
line 27why, one step back27 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 28–36why, one step back28 -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[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 47–81why, one step back47}4849__fzf_orig_completion_filter() {50 sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2"; [[ "\1" = *" -o nospace "* ]] \&\& [[ ! "$__fzf_nospace_commands" = *" \3 "* ]] \&\& __fzf_nospace_commands="$__fzf_nospace_commands \3 ";/' |51 awk -F= '{OFS = FS} {gsub(/[^A-Za-z0-9_= ;]/, "_", $1);}1'52}5354_fzf_opts_completion() {55 local cur prev opts56 COMPREPLY=()57 cur="${COMP_WORDS[COMP_CWORD]}"58 prev="${COMP_WORDS[COMP_CWORD-1]}"59 opts="60 -x --extended61 -e --exact62 --algo63 -i +i64 -n --nth65 --with-nth66 -d --delimiter67 +s --no-sort68 --tac69 --tiebreak70 -m --multi71 --no-mouse72 --bind73 --cycle74 --no-hscroll75 --jump-labels76 --height77 --literal78 --reverse79 --margin80 --inline-info81 --promptMake pointer and multi-select marker customizable (#1844)Hiroki Konishibefore adoption2a60edc
lines 82–83why, one step back82 --pointer83 --marker[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 84–134why, one step back84 --header85 --header-lines86 --ansi87 --tabstop88 --color89 --no-bold90 --history91 --history-size92 --preview93 --preview-window94 -q --query95 -1 --select-196 -0 --exit-097 -f --filter98 --print-query99 --expect100 --sync"101102 case "${prev}" in103 --tiebreak)104 COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )105 return 0106 ;;107 --color)108 COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )109 return 0110 ;;111 --history)112 COMPREPLY=()113 return 0114 ;;115 esac116117 if [[ "$cur" =~ ^-|\+ ]]; then118 COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )119 return 0120 fi121122 return 0123}124125_fzf_handle_dynamic_completion() {126 local cmd orig_var orig ret orig_cmd orig_complete127 cmd="$1"128 shift129 orig_cmd="$1"130 orig_var="_fzf_orig_completion_$cmd"131 orig="${!orig_var##*#}"132 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then133 $orig "$@"134 elif [ -n "$_fzf_completion_loader" ]; then[bash-completion] Fix custom completion with dynamic loader enabled for non-standard command names (#1564)Jesus Brialesbefore adoption03e8ed4
line 135why, one step back135 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 136–138why, one step back136 _completion_loader "$@"137 ret=$?138 # _completion_loader may not have updated completion for the command[bash-completion] Fix custom completion with dynamic loader enabled for non-standard command names (#1564)Jesus Brialesbefore adoption03e8ed4
line 139why, one step back139 if [ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]; then[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 140–151why, one step back140 eval "$(complete | command grep " -F.* $orig_cmd$" | __fzf_orig_completion_filter)"141 if [[ "$__fzf_nospace_commands" = *" $orig_cmd "* ]]; then142 eval "${orig_complete/ -F / -o nospace -F }"143 else144 eval "$orig_complete"145 fi146 fi147 return $ret148 fi149}150151__fzf_generic_path_completion() {[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 152why, one step back152 local cur base dir leftover matches trigger cmd[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 153–167why, one step back153 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"154 COMPREPLY=()155 trigger=${FZF_COMPLETION_TRIGGER-'**'}156 cur="${COMP_WORDS[COMP_CWORD]}"157 if [[ "$cur" == *"$trigger" ]]; then158 base=${cur:0:${#cur}-${#trigger}}159 eval "base=$base"160161 [[ $base = *"/"* ]] && dir="$base"162 while true; do163 if [ -z "$dir" ] || [ -d "$dir" ]; then164 leftover=${base/#"$dir"}165 leftover=${leftover/#\/}166 [ -z "$dir" ] && dir='.'167 [ "$dir" != "/" ] && dir="${dir/%\//}"[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 168why, one step back168 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[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 169–192why, one step back169 printf "%q$3 " "$item"170 done)171 matches=${matches% }172 [[ -z "$3" ]] && [[ "$__fzf_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "173 if [ -n "$matches" ]; then174 COMPREPLY=( "$matches" )175 else176 COMPREPLY=( "$cur" )177 fi178 printf '\e[5n'179 return 0180 fi181 dir=$(dirname "$dir")182 [[ "$dir" =~ /$ ]] || dir="$dir"/183 done184 else185 shift186 shift187 shift188 _fzf_handle_dynamic_completion "$cmd" "$@"189 fi190}191192_fzf_complete() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
lines 193–213why, one step back193 # Split arguments around --194 local args rest str_arg i sep195 args=("$@")196 sep=197 for i in "${!args[@]}"; do198 if [[ "${args[$i]}" = -- ]]; then199 sep=$i200 break201 fi202 done203 if [[ -n "$sep" ]]; then204 str_arg=205 rest=("${args[@]:$((sep + 1)):${#args[@]}}")206 args=("${args[@]:0:$sep}")207 else208 str_arg=$1209 args=()210 shift211 rest=("$@")212 fi213[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 214why, one step back214 local cur selected trigger cmd post[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 215–223why, one step back215 post="$(caller 0 | awk '{print $2}')_post"216 type -t "$post" > /dev/null 2>&1 || post=cat217218 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"219 trigger=${FZF_COMPLETION_TRIGGER-'**'}220 cur="${COMP_WORDS[COMP_CWORD]}"221 if [[ "$cur" == *"$trigger" ]]; then222 cur=${cur:0:${#cur}-${#trigger}}223Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 224why, one step back224 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' ' ')[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 225–227why, one step back225 selected=${selected% } # Strip trailing space not to repeat "-o nospace"226 if [ -n "$selected" ]; then227 COMPREPLY=("$selected")[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be
lines 228–229why, one step back228 else229 COMPREPLY=("$cur")[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
line 230why, one step back230 fi[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be
lines 231–232why, one step back231 printf '\e[5n'232 return 0[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
line 233why, one step back233 elseChange custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 234why, one step back234 _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 235–251why, one step back235 fi236}237238_fzf_path_completion() {239 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"240}241242# Deprecated. No file only completion.243_fzf_file_completion() {244 _fzf_path_completion "$@"245}246247_fzf_dir_completion() {248 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"249}250251_fzf_complete_kill() {[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 252–258why, one step back252 local trigger=${FZF_COMPLETION_TRIGGER-'**'}253 local cur="${COMP_WORDS[COMP_CWORD]}"254 if [[ -z "$cur" ]]; then255 COMP_WORDS[$COMP_CWORD]=$trigger256 elif [[ "$cur" != *"$trigger" ]]; then257 return 1258 fi[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
line 259why, one step back259[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 260–261why, one step back260 _fzf_proc_completion "$@"261}[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
line 262why, one step back262[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 263–266why, one step back263_fzf_proc_completion() {264 _fzf_complete -m --preview 'echo {}' --preview-window down:3:wrap --min-height 15 -- "$@" < <(265 command ps -ef | sed 1d266 )[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 267–268why, one step back267}268[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
lines 269–272why, one step back269_fzf_proc_completion_post() {270 awk '{print $2}'271}272[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 273why, one step back273_fzf_host_completion() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 274why, one step back274 _fzf_complete +m -- "$@" < <([ssh completion] Skip only aliases matching * (#1788)Henré Bothabefore adoption5e42b1c
line 275why, one step back275 cat <(cat ~/.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 '[*?]') \[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 276–281why, one step back276 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \277 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |278 awk '{if (length($2) > 0) {print $2}}' | sort -u279 )280}281[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 282why, one step back282_fzf_var_completion() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 283why, one step back283 _fzf_complete -m -- "$@" < <([install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 284–287why, one step back284 declare -xp | sed 's/=.*//' | sed 's/.* //'285 )286}287[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 288why, one step back288_fzf_alias_completion() {Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608
line 289why, one step back289 _fzf_complete -m -- "$@" < <([install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 290–309why, one step back290 alias | sed 's/=.*//' | sed 's/.* //'291 )292}293294# fzf options295complete -o default -F _fzf_opts_completion fzf296297d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"298a_cmds="299 awk cat diff diff3300 emacs emacsclient ex file ftp g++ gcc gvim head hg java301 javac ld less more mvim nvim patch perl python ruby302 sed sftp sort source tail tee uniq vi view vim wc xdg-open303 basename bunzip2 bzip2 chmod chown curl cp dirname du304 find git grep gunzip gzip hg jar305 ln ls mv open rm rsync scp306 svn tar unzip zip"307308# Preserve existing completion309eval "$(complete |Remove dead codeJunegunn Choibefore adoption6e464eb
line 310why, one step back310 sed -E '/-F/!d; / _fzf/d; '"/ ($(echo $d_cmds $a_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 311–316why, one step back311 __fzf_orig_completion_filter)"312313if type _completion_loader > /dev/null 2>&1; then314 _fzf_completion_loader=1315fi316[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 317why, one step back317__fzf_defc() {[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 318–333why, one step back318 local cmd func opts orig_var orig def319 cmd="$1"320 func="$2"321 opts="$3"322 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"323 orig="${!orig_var}"324 if [ -n "$orig" ]; then325 printf -v def "$orig" "$func"326 eval "$def"327 else328 complete -F "$func" $opts "$cmd"329 fi330}331332# Anything333for cmd in $a_cmds; do[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 334why, one step back334 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 335–338why, one step back335done336337# Directory338for cmd in $d_cmds; do[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 339why, one step back339 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
lines 340–341why, one step back340done341[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
line 342why, one step back342# Kill completion (supports empty completion trigger)[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 343why, one step back343complete -F _fzf_complete_kill -o default -o bashdefault kill[install] Escape spaces in installation directoryJunegunn Choibefore adoption8e34e6f
line 344why, one step back344Remove dead codeJunegunn Choibefore adoption6e464eb
line 345why, one step back345unset cmd d_cmds a_cmds[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5
line 346why, one step back346[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
line 347why, one step back347_fzf_setup_completion() {[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
lines 348–351why, one step back348 local kind fn cmd349 kind=$1350 fn=_fzf_${1}_completion351 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279
line 352why, one step back352 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
lines 353–355why, one step back353 return 1354 fi355 shift[bash-completion] Minor optimizationJunegunn Choibefore adoption54891d1
line 356why, one step back356 eval "$(complete -p "$@" 2> /dev/null | grep -v "$fn" | __fzf_orig_completion_filter)"[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
line 357why, one step back357 for cmd in "$@"; do[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 358why, one step back358 case "$kind" in[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
lines 359–362why, one step back359 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;360 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;361 alias) __fzf_defc "$cmd" "$fn" "-a" ;;362 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 363why, one step back363 esac[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
lines 364–365why, one step back364 done365}[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
lines 366–370why, one step back366367# Environment variables / Aliases / Hosts368_fzf_setup_completion 'var' export unset369_fzf_setup_completion 'alias' unalias370_fzf_setup_completion 'host' ssh telnet[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
line 371why, one step back371[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5
line 372why, one step back372fi