completion.bash
This file
Path
shell/completion.bash
345 lines · 9.2 KiB · Shell
Last changed by
[completion] Use file redirection instead of pipeJunegunn Choibefore adoptionbaf882a
History of this fileTake it
The walk stopped after 200 commits: lines older than ddd2a10 point at it.
[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 1–11why, one step back1# ____ ____2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/-completion.bash6#7# - $FZF_TMUX (default: 0)8# - $FZF_TMUX_HEIGHT (default: '40%')9# - $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[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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 \) \[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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 \[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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–46why, one step back37__fzf_comprun() {38 if [ "$(type -t _fzf_comprun 2>&1)" = function ]; then39 _fzf_comprun "$@"40 elif [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]; then41 shift42 fzf-tmux -d "${FZF_TMUX_HEIGHT:-40%}" "$@"43 else44 shift45 fzf "$@"46 fi[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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)[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 153–160why, 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"160[bash/zsh] Fix missing fuzzy completions (#1303)ptzzbefore adoption2b19c0b
line 161why, one step back161 [[ $base = *"/"* ]] && dir="$base"[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 162–167why, one step back162 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[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
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() {[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
line 193why, one step back193 local cur selected trigger cmd post[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 194–202why, one step back194 post="$(caller 0 | awk '{print $2}')_post"195 type -t "$post" > /dev/null 2>&1 || post=cat196197 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"198 trigger=${FZF_COMPLETION_TRIGGER-'**'}199 cur="${COMP_WORDS[COMP_CWORD]}"200 if [[ "$cur" == *"$trigger" ]]; then201 cur=${cur:0:${#cur}-${#trigger}}202[completion] Use file redirection instead of pipeJunegunn Choibefore adoptionbaf882a
line 203why, one step back203 selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $1" __fzf_comprun "$2" -q "$cur" | $post | tr '\n' ' ')[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 204–206why, one step back204 selected=${selected% } # Strip trailing space not to repeat "-o nospace"205 if [ -n "$selected" ]; then206 COMPREPLY=("$selected")[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be
lines 207–208why, one step back207 else208 COMPREPLY=("$cur")[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
line 209why, one step back209 fi[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be
lines 210–211why, one step back210 printf '\e[5n'211 return 0[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 212–233why, one step back212 else213 shift214 _fzf_handle_dynamic_completion "$cmd" "$@"215 fi216}217218_fzf_path_completion() {219 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"220}221222# Deprecated. No file only completion.223_fzf_file_completion() {224 _fzf_path_completion "$@"225}226227_fzf_dir_completion() {228 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"229}230231_fzf_complete_kill() {232 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1233[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415
lines 234–235why, one step back234 local selected235 selected=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS --preview 'echo {}' --preview-window down:3:wrap" __fzf_comprun "kill" -m | awk '{print $2}' | tr '\n' ' ')[bash] Strip trailing whitespace on kill completionJunegunn Choibefore adoption9897ee9
line 236why, one step back236 selected=${selected% }[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 237–244why, one step back237 printf '\e[5n'238239 if [ -n "$selected" ]; then240 COMPREPLY=( "$selected" )241 return 0242 fi243}244[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 245why, one step back245_fzf_host_completion() {[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
line 246why, one step back246 _fzf_complete '+m' "$@" < <([ssh completion] Skip only aliases matching * (#1788)Henré Bothabefore adoption5e42b1c
line 247why, one step back247 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 '[*?]') \[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 248–253why, one step back248 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \249 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |250 awk '{if (length($2) > 0) {print $2}}' | sort -u251 )252}253[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 254why, one step back254_fzf_var_completion() {[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 255–259why, one step back255 _fzf_complete '-m' "$@" < <(256 declare -xp | sed 's/=.*//' | sed 's/.* //'257 )258}259[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 260why, one step back260_fzf_alias_completion() {[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 261–278why, one step back261 _fzf_complete '-m' "$@" < <(262 alias | sed 's/=.*//' | sed 's/.* //'263 )264}265266# fzf options267complete -o default -F _fzf_opts_completion fzf268269d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"270a_cmds="271 awk cat diff diff3272 emacs emacsclient ex file ftp g++ gcc gvim head hg java273 javac ld less more mvim nvim patch perl python ruby274 sed sftp sort source tail tee uniq vi view vim wc xdg-open275 basename bunzip2 bzip2 chmod chown curl cp dirname du276 find git grep gunzip gzip hg jar277 ln ls mv open rm rsync scp278 svn tar unzip zip"[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 279why, one step back279x_cmds="kill"[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 280–289why, one step back280281# Preserve existing completion282eval "$(complete |283 sed -E '/-F/!d; / _fzf/d; '"/ ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |284 __fzf_orig_completion_filter)"285286if type _completion_loader > /dev/null 2>&1; then287 _fzf_completion_loader=1288fi289[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 290why, one step back290__fzf_defc() {[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 291–306why, one step back291 local cmd func opts orig_var orig def292 cmd="$1"293 func="$2"294 opts="$3"295 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"296 orig="${!orig_var}"297 if [ -n "$orig" ]; then298 printf -v def "$orig" "$func"299 eval "$def"300 else301 complete -F "$func" $opts "$cmd"302 fi303}304305# Anything306for cmd in $a_cmds; do[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 307why, one step back307 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 308–311why, one step back308done309310# Directory311for cmd in $d_cmds; do[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 312why, one step back312 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 313–315why, one step back313done314315# Kill completion[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 316why, one step back316complete -F _fzf_complete_kill -o default -o bashdefault kill[fzf-tmux] Restore tmux window options (#1272)Heinrich Krugerbefore adoptionddd2a10
lines 317–318why, one step back317318unset cmd d_cmds a_cmds x_cmds[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5
line 319why, one step back319[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
line 320why, one step back320_fzf_setup_completion() {[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
lines 321–324why, one step back321 local kind fn cmd322 kind=$1323 fn=_fzf_${1}_completion324 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
line 325why, one step back325 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host COMMANDS..."[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
lines 326–328why, one step back326 return 1327 fi328 shift[bash-completion] Minor optimizationJunegunn Choibefore adoption54891d1
line 329why, one step back329 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 330why, one step back330 for cmd in "$@"; do[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 331why, one step back331 case "$kind" in[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
lines 332–335why, one step back332 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;333 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;334 alias) __fzf_defc "$cmd" "$fn" "-a" ;;335 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf
line 336why, one step back336 esac[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
lines 337–338why, one step back337 done338}[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
lines 339–343why, one step back339340# Environment variables / Aliases / Hosts341_fzf_setup_completion 'var' export unset342_fzf_setup_completion 'alias' unalias343_fzf_setup_completion 'host' ssh telnet[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899
line 344why, one step back344[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5
line 345why, one step back345fi