Skip to content
Myr

completion.bash

This file

Path

shell/completion.bash

338 lines · 9.1 KiB · Shell

Last changed by

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

History of this file

Take it

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 14–18why, 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" \

[completion] Ignore hg repos (#1777)John Purnellbefore adoption357e82e

line 19why, one step back
19 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 20–26why, one step back
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" \

[completion] Ignore hg repos (#1777)John Purnellbefore adoption357e82e

line 27why, one step back
27 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 28–41why, one step back
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'

Add replace-query actionJunegunn Choibefore adoptionc20954f

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

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)

Add replace-query actionJunegunn Choibefore adoptionc20954f

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

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"

Add replace-query actionJunegunn Choibefore adoptionc20954f

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 "

Add replace-query actionJunegunn Choibefore adoptionc20954f

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")

Add replace-query actionJunegunn Choibefore adoptionc20954f

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 205–237why, 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)"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' ' ')230 printf '\e[5n'231232 if [ -n "$selected" ]; then233 COMPREPLY=( "$selected" )234 return 0235 fi236}237

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

line 239why, one step back
239 _fzf_complete '+m' "$@" < <(

[ssh completion] Skip only aliases matching * (#1788)Henré Bothabefore adoption5e42b1c

line 240why, one step back
240 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 '[*?]') \

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 241–246why, one step back
241 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
242 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |243 awk '{if (length($2) > 0) {print $2}}' | sort -u244 )245}246

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

line 247why, one step back
247_fzf_var_completion() {

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 248–252why, one step back
248 _fzf_complete '-m' "$@" < <(
249 declare -xp | sed 's/=.*//' | sed 's/.* //'250 )251}252

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

line 253why, one step back
253_fzf_alias_completion() {

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 254–271why, one step back
254 _fzf_complete '-m' "$@" < <(
255 alias | sed 's/=.*//' | sed 's/.* //'256 )257}258259# fzf options260complete -o default -F _fzf_opts_completion fzf261262d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"263a_cmds="264 awk cat diff diff3265 emacs emacsclient ex file ftp g++ gcc gvim head hg java266 javac ld less more mvim nvim patch perl python ruby267 sed sftp sort source tail tee uniq vi view vim wc xdg-open268 basename bunzip2 bzip2 chmod chown curl cp dirname du269 find git grep gunzip gzip hg jar270 ln ls mv open rm rsync scp271 svn tar unzip zip"

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

line 272why, one step back
272x_cmds="kill"

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 273–274why, one step back
273
274# Preserve existing completion

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

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

line 276why, one step back
276 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 277why, one step back
277 __fzf_orig_completion_filter)"

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 278–282why, one step back
278
279if type _completion_loader > /dev/null 2>&1; then280 _fzf_completion_loader=1281fi282

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

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 284–299why, one step back
284 local cmd func opts orig_var orig def
285 cmd="$1"286 func="$2"287 opts="$3"288 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"289 orig="${!orig_var}"290 if [ -n "$orig" ]; then291 printf -v def "$orig" "$func"292 eval "$def"293 else294 complete -F "$func" $opts "$cmd"295 fi296}297298# Anything299for cmd in $a_cmds; do

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

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 301–304why, one step back
301done
302303# Directory304for cmd in $d_cmds; do

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

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

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 306–308why, one step back
306done
307308# Kill completion

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

line 309why, one step back
309complete -F _fzf_complete_kill -o default -o bashdefault kill

Add replace-query actionJunegunn Choibefore adoptionc20954f

lines 310–311why, one step back
310
311unset cmd d_cmds a_cmds x_cmds

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

line 312why, one step back
312

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

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

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

lines 314–317why, one step back
314 local kind fn cmd
315 kind=$1316 fn=_fzf_${1}_completion317 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

line 318why, one step back
318 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host COMMANDS..."

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

lines 319–323why, one step back
319 return 1
320 fi321 shift322 for cmd in "$@"; do323 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

line 324why, one step back
324 case "$kind" in

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

lines 325–328why, one step back
325 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;
326 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;327 alias) __fzf_defc "$cmd" "$fn" "-a" ;;328 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;

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

line 329why, one step back
329 esac

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

lines 330–331why, one step back
330 done
331}

[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2

lines 332–336why, one step back
332
333# Environment variables / Aliases / Hosts334_fzf_setup_completion 'var' export unset335_fzf_setup_completion 'alias' unalias336_fzf_setup_completion 'host' ssh telnet

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

line 337why, one step back
337

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

line 338why, one step back
338fi