Skip to content
Myr

completion.bash

This file

Path

shell/completion.bash

353 lines · 9.5 KiB · Shell

Last changed by

[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be

History of this file

Take it

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

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 1–11why, one step back
1# ____ ____
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 back
12if [[ $- =~ i ]]; then
13

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 14–41why, one step back
14# 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 .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 .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__fzfcmd_complete() {38 [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] &&39 echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"40}41

[bash] Fix extra space issue of dynamic completion with 'nospace'Junegunn Choibefore adoptioncce6aef

lines 42–43why, one step back
42__fzf_orig_completion_filter() {
43 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 ";/' |

[bash] Fix infinite loop on tab completionJunegunn Choibefore adoption87504a5

line 44why, one step back
44 awk -F= '{OFS = FS} {gsub(/[^A-Za-z0-9_= ;]/, "_", $1);}1'

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 45–116why, one step back
45}
4647_fzf_opts_completion() {48 local cur prev opts49 COMPREPLY=()50 cur="${COMP_WORDS[COMP_CWORD]}"51 prev="${COMP_WORDS[COMP_CWORD-1]}"52 opts="53 -x --extended54 -e --exact55 --algo56 -i +i57 -n --nth58 --with-nth59 -d --delimiter60 +s --no-sort61 --tac62 --tiebreak63 -m --multi64 --no-mouse65 --bind66 --cycle67 --no-hscroll68 --jump-labels69 --height70 --literal71 --reverse72 --margin73 --inline-info74 --prompt75 --header76 --header-lines77 --ansi78 --tabstop79 --color80 --no-bold81 --history82 --history-size83 --preview84 --preview-window85 -q --query86 -1 --select-187 -0 --exit-088 -f --filter89 --print-query90 --expect91 --sync"9293 case "${prev}" in94 --tiebreak)95 COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )96 return 097 ;;98 --color)99 COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )100 return 0101 ;;102 --history)103 COMPREPLY=()104 return 0105 ;;106 esac107108 if [[ "$cur" =~ ^-|\+ ]]; then109 COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )110 return 0111 fi112113 return 0114}115116_fzf_handle_dynamic_completion() {

[bash-completion] Fix custom completion with dynamic loader enabledJunegunn Choibefore adoptiond6588fc

line 117why, one step back
117 local cmd orig_var orig ret orig_cmd orig_complete

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 118–125why, one step back
118 cmd="$1"
119 shift120 orig_cmd="$1"121 orig_var="_fzf_orig_completion_$cmd"122 orig="${!orig_var##*#}"123 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then124 $orig "$@"125 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 126why, one step back
126 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 127–128why, one step back
127 _completion_loader "$@"
128 ret=$?

[bash-completion] Fix custom completion with dynamic loader enabledJunegunn Choibefore adoptiond6588fc

line 129why, one step back
129 # _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 130why, one step back
130 if [ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]; then

[bash] Fix extra space issue of dynamic completion with 'nospace'Junegunn Choibefore adoptioncce6aef

lines 131–136why, one step back
131 eval "$(complete | command grep " -F.* $orig_cmd$" | __fzf_orig_completion_filter)"
132 if [[ "$__fzf_nospace_commands" = *" $orig_cmd "* ]]; then133 eval "${orig_complete/ -F / -o nospace -F }"134 else135 eval "$orig_complete"136 fi

[bash-completion] Fix custom completion with dynamic loader enabledJunegunn Choibefore adoptiond6588fc

line 137why, one step back
137 fi

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 138–152why, one step back
138 return $ret
139 fi140}141142__fzf_generic_path_completion() {143 local cur base dir leftover matches trigger cmd fzf144 fzf="$(__fzfcmd_complete)"145 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"146 COMPREPLY=()147 trigger=${FZF_COMPLETION_TRIGGER-'**'}148 cur="${COMP_WORDS[COMP_CWORD]}"149 if [[ "$cur" == *"$trigger" ]]; then150 base=${cur:0:${#cur}-${#trigger}}151 eval "base=$base"152

[bash/zsh] Fix missing fuzzy completions (#1303)ptzzbefore adoption2b19c0b

line 153why, one step back
153 [[ $base = *"/"* ]] && dir="$base"

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 154–163why, one step back
154 while true; do
155 if [ -z "$dir" ] || [ -d "$dir" ]; then156 leftover=${base/#"$dir"}157 leftover=${leftover/#\/}158 [ -z "$dir" ] && dir='.'159 [ "$dir" != "/" ] && dir="${dir/%\//}"160 matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $2 -q "$leftover" | while read -r item; do161 printf "%q$3 " "$item"162 done)163 matches=${matches% }

[bash] Fix extra space issue of dynamic completion with 'nospace'Junegunn Choibefore adoptioncce6aef

line 164why, one step back
164 [[ -z "$3" ]] && [[ "$__fzf_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 165–199why, one step back
165 if [ -n "$matches" ]; then
166 COMPREPLY=( "$matches" )167 else168 COMPREPLY=( "$cur" )169 fi170 printf '\e[5n'171 return 0172 fi173 dir=$(dirname "$dir")174 [[ "$dir" =~ /$ ]] || dir="$dir"/175 done176 else177 shift178 shift179 shift180 _fzf_handle_dynamic_completion "$cmd" "$@"181 fi182}183184_fzf_complete() {185 local cur selected trigger cmd fzf post186 post="$(caller 0 | awk '{print $2}')_post"187 type -t "$post" > /dev/null 2>&1 || post=cat188 fzf="$(__fzfcmd_complete)"189190 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"191 trigger=${FZF_COMPLETION_TRIGGER-'**'}192 cur="${COMP_WORDS[COMP_CWORD]}"193 if [[ "$cur" == *"$trigger" ]]; then194 cur=${cur:0:${#cur}-${#trigger}}195196 selected=$(cat | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $1 -q "$cur" | $post | tr '\n' ' ')197 selected=${selected% } # Strip trailing space not to repeat "-o nospace"198 if [ -n "$selected" ]; then199 COMPREPLY=("$selected")

[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be

lines 200–201why, one step back
200 else
201 COMPREPLY=("$cur")

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

line 202why, one step back
202 fi

[bash-completion] Properly handle exit eventJunegunn Choibefore adoptiona5a97be

lines 203–204why, one step back
203 printf '\e[5n'
204 return 0

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 205–228why, one step back
205 else
206 shift207 _fzf_handle_dynamic_completion "$cmd" "$@"208 fi209}210211_fzf_path_completion() {212 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"213}214215# Deprecated. No file only completion.216_fzf_file_completion() {217 _fzf_path_completion "$@"218}219220_fzf_dir_completion() {221 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"222}223224_fzf_complete_kill() {225 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1226227 local selected fzf228 fzf="$(__fzfcmd_complete)"

[completion] Ensure ps called as command (#1098)Igor Urazovbefore adoptioneaf6eb8

line 229why, one step back
229 selected=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS --preview 'echo {}' --preview-window down:3:wrap $FZF_COMPLETION_OPTS" $fzf -m | awk '{print $2}' | tr '\n' ' ')

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 230–246why, one step back
230 printf '\e[5n'
231232 if [ -n "$selected" ]; then233 COMPREPLY=( "$selected" )234 return 0235 fi236}237238_fzf_complete_telnet() {239 _fzf_complete '+m' "$@" < <(240 command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |241 awk '{if (length($2) > 0) {print $2}}' | sort -u242 )243}244245_fzf_complete_ssh() {246 _fzf_complete '+m' "$@" < <(

[completion] Look up on ~/.ssh/config.d/* files when doing ssh host complete (#1420)Rui Coelhobefore adoption3bd8441

line 247why, one step back
247 cat <(cat ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host ' | command grep -v '[*?]' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \

[completion] Fix known_hosts completion for custom port number (#1092)Andrey Chernihbefore adoption80a21f7

line 248why, one step back
248 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 249–287why, one step back
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}253254_fzf_complete_unset() {255 _fzf_complete '-m' "$@" < <(256 declare -xp | sed 's/=.*//' | sed 's/.* //'257 )258}259260_fzf_complete_export() {261 _fzf_complete '-m' "$@" < <(262 declare -xp | sed 's/=.*//' | sed 's/.* //'263 )264}265266_fzf_complete_unalias() {267 _fzf_complete '-m' "$@" < <(268 alias | sed 's/=.*//' | sed 's/.* //'269 )270}271272# fzf options273complete -o default -F _fzf_opts_completion fzf274275d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"276a_cmds="277 awk cat diff diff3278 emacs emacsclient ex file ftp g++ gcc gvim head hg java279 javac ld less more mvim nvim patch perl python ruby280 sed sftp sort source tail tee uniq vi view vim wc xdg-open281 basename bunzip2 bzip2 chmod chown curl cp dirname du282 find git grep gunzip gzip hg jar283 ln ls mv open rm rsync scp284 svn tar unzip zip"285x_cmds="kill ssh telnet unset unalias export"286287# Preserve existing completion

[bash] Fix extra space issue of dynamic completion with 'nospace'Junegunn Choibefore adoptioncce6aef

line 288why, one step back
288eval "$(complete |

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

line 289why, one step back
289 sed -E '/-F/!d; / _fzf/d; '"/ ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |

[bash] Fix extra space issue of dynamic completion with 'nospace'Junegunn Choibefore adoptioncce6aef

line 290why, one step back
290 __fzf_orig_completion_filter)"

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 291–295why, one step back
291
292if type _completion_loader > /dev/null 2>&1; then293 _fzf_completion_loader=1294fi295

[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf

line 296why, one step back
296__fzf_defc() {

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 297–312why, one step back
297 local cmd func opts orig_var orig def
298 cmd="$1"299 func="$2"300 opts="$3"301 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"302 orig="${!orig_var}"303 if [ -n "$orig" ]; then304 printf -v def "$orig" "$func"305 eval "$def"306 else307 complete -F "$func" $opts "$cmd"308 fi309}310311# Anything312for cmd in $a_cmds; do

[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf

line 313why, one step back
313 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 314–317why, one step back
314done
315316# Directory317for cmd in $d_cmds; do

[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf

line 318why, one step back
318 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"

[install] Ignore user-defined grep aliasesJunegunn Choibefore adoption20cdbac

lines 319–333why, one step back
319done
320321# Kill completion322complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill323324# Host completion325complete -F _fzf_complete_ssh -o default -o bashdefault ssh326complete -F _fzf_complete_telnet -o default -o bashdefault telnet327328# Environment variables / Aliases329complete -F _fzf_complete_unset -o default -o bashdefault unset330complete -F _fzf_complete_export -o default -o bashdefault export331complete -F _fzf_complete_unalias -o default -o bashdefault unalias332333unset cmd d_cmds a_cmds x_cmds

[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5

line 334why, one step back
334

[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899

line 335why, one step back
335_fzf_setup_completion() {

[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf

lines 336–339why, one step back
336 local kind fn cmd
337 kind=$1338 fn=_fzf_${1}_completion339 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then

[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899

lines 340–345why, one step back
340 echo "usage: ${FUNCNAME[0]} path|dir COMMANDS..."
341 return 1342 fi343 shift344 for cmd in "$@"; do345 eval "$(complete -p "$cmd" 2> /dev/null | grep -v "$fn" | __fzf_orig_completion_filter)"

[bash-completion] _fzf_setup_completion to retain previous optionsJunegunn Choibefore adoption5c7dcaf

lines 346–349why, one step back
346 case "$kind" in
347 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;348 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;349 esac

[bash-completion] Add _fzf_setup_completion to enable fuzzy completionJunegunn Choibefore adoption5095899

lines 350–352why, one step back
350 done
351}352

[shell] Skip loading completion code on non-interactive shellJunegunn Choibefore adoption8cf45a5

line 353why, one step back
353fi