completion.bash
This file
Path
shell/completion.bash
314 lines · 8.0 KiB · Shell
Last changed by
[shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
History of this fileTake it
The walk stopped after 200 commits: lines older than d4e2670 point at it.
GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 1–12why, one step back1#!/bin/bash2# ____ ____3# / __/___ / __/4# / /_/_ / / /_5# / __/ / /_/ __/6# /_/ /___/_/-completion.bash7#8# - $FZF_TMUX (default: 1)9# - $FZF_TMUX_HEIGHT (default: '40%')10# - $FZF_COMPLETION_TRIGGER (default: '**')11# - $FZF_COMPLETION_OPTS (default: empty)12Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
lines 13–16why, one step back13# To use custom commands instead of find, override _fzf_compgen_{path,dir}14if ! declare -f _fzf_compgen_path > /dev/null; then15 _fzf_compgen_path() {16 echo "$1"[shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
line 17why, one step back17 command find -L "$1" \Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
lines 18–24why, one step back18 -name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \19 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'20 }21fi2223if ! declare -f _fzf_compgen_dir > /dev/null; then24 _fzf_compgen_dir() {[shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
line 25why, one step back25 command find -L "$1" \Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
lines 26–32why, one step back26 -name .git -prune -o -name .svn -prune -o -type d \27 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'28 }29fi3031###########################################################32GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 33–77why, one step back33_fzf_orig_completion_filter() {34 sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2";/' |35 awk -F= '{gsub(/[^a-z0-9_= ;]/, "_", $1); print $1"="$2}'36}3738_fzf_opts_completion() {39 local cur prev opts40 COMPREPLY=()41 cur="${COMP_WORDS[COMP_CWORD]}"42 prev="${COMP_WORDS[COMP_CWORD-1]}"43 opts="44 -x --extended45 -e --exact46 -i +i47 -n --nth48 -d --delimiter49 +s --no-sort50 --tac51 --tiebreak52 --bind53 -m --multi54 --no-mouse55 --color56 --black57 --reverse58 --no-hscroll59 --inline-info60 --prompt61 -q --query62 -1 --select-163 -0 --exit-064 -f --filter65 --print-query66 --expect67 --toggle-sort68 --sync69 --cycle70 --history71 --history-size72 --header73 --header-lines74 --margin"7576 case "${prev}" in77 --tiebreak)[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 78why, one step back78 COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 79–81why, one step back79 return 080 ;;81 --color)[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 82why, one step back82 COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 83–90why, one step back83 return 084 ;;85 --history)86 COMPREPLY=()87 return 088 ;;89 esac90[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
lines 91–92why, one step back91 if [[ "$cur" =~ ^-|\+ ]]; then92 COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 93–110why, one step back93 return 094 fi9596 return 097}9899_fzf_handle_dynamic_completion() {100 local cmd orig_var orig ret orig_cmd101 cmd="$1"102 shift103 orig_cmd="$1"104 orig_var="_fzf_orig_completion_$cmd"105 orig="${!orig_var##*#}"106 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then107 $orig "$@"108 elif [ -n "$_fzf_completion_loader" ]; then109 _completion_loader "$@"110 ret=$?[shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
line 111why, one step back111 eval "$(complete | command grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)"[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 112why, one step back112 source "${BASH_SOURCE[0]}"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 113–118why, one step back113 return $ret114 fi115}116117__fzf_generic_path_completion() {118 local cur base dir leftover matches trigger cmd fzf[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
lines 119–120why, one step back119 [ "${FZF_TMUX:-1}" != 0 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"120 cmd=$(echo "${COMP_WORDS[0]}" | sed 's/[^a-z0-9_=]/_/g')GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 121–123why, one step back121 COMPREPLY=()122 trigger=${FZF_COMPLETION_TRIGGER-'**'}123 cur="${COMP_WORDS[COMP_CWORD]}"[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 124why, one step back124 if [[ "$cur" == *"$trigger" ]]; thenGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
line 125why, one step back125 base=${cur:0:${#cur}-${#trigger}}[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 126why, one step back126 eval "base=$base"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 127–128why, one step back127128 dir="$base"[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
lines 129–130why, one step back129 while true; do130 if [ -z "$dir" ] || [ -d "$dir" ]; thenGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 131–132why, one step back131 leftover=${base/#"$dir"}132 leftover=${leftover/#\/}Merge pull request #458 from frizinak/fix-autocomplete-absJunegunn Choibefore adoptiona9aa263
lines 133–134why, one step back133 [ -z "$dir" ] && dir='.'134 [ "$dir" != "/" ] && dir="${dir/%\//}"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
line 135why, one step back135 tput sc[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 136why, one step back136 matches=$(eval "$1 $(printf %q "$dir")" | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read -r item; doGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 137–159why, one step back137 printf "%q$3 " "$item"138 done)139 matches=${matches% }140 if [ -n "$matches" ]; then141 COMPREPLY=( "$matches" )142 else143 COMPREPLY=( "$cur" )144 fi145 tput rc146 return 0147 fi148 dir=$(dirname "$dir")149 [[ "$dir" =~ /$ ]] || dir="$dir"/150 done151 else152 shift153 shift154 shift155 _fzf_handle_dynamic_completion "$cmd" "$@"156 fi157}158159_fzf_complete() {[completion] _fzf_complete_COMMAND_post for post processingJunegunn Choibefore adoption8206746
lines 160–161why, one step back160 local cur selected trigger cmd fzf post161 post="$(caller 0 | awk '{print $2}')_post"[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
lines 162–163why, one step back162 type -t "$post" > /dev/null 2>&1 || post=cat163 [ "${FZF_TMUX:-1}" != 0 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
line 164why, one step back164[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 165why, one step back165 cmd=$(echo "${COMP_WORDS[0]}" | sed 's/[^a-z0-9_=]/_/g')GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 166–167why, one step back166 trigger=${FZF_COMPLETION_TRIGGER-'**'}167 cur="${COMP_WORDS[COMP_CWORD]}"[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 168why, one step back168 if [[ "$cur" == *"$trigger" ]]; thenGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 169–171why, one step back169 cur=${cur:0:${#cur}-${#trigger}}170171 tput sc[completion] _fzf_complete_COMMAND_post for post processingJunegunn Choibefore adoption8206746
line 172why, one step back172 selected=$(cat | $fzf $FZF_COMPLETION_OPTS $1 -q "$cur" | $post | tr '\n' ' ')GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 173–186why, one step back173 selected=${selected% } # Strip trailing space not to repeat "-o nospace"174 tput rc175176 if [ -n "$selected" ]; then177 COMPREPLY=("$selected")178 return 0179 fi180 else181 shift182 _fzf_handle_dynamic_completion "$cmd" "$@"183 fi184}185186_fzf_path_completion() {Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
line 187why, one step back187 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 188–189why, one step back188}189Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
line 190why, one step back190# Deprecated. No file only completion.GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
line 191why, one step back191_fzf_file_completion() {Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
line 192why, one step back192 _fzf_path_completion "$@"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 193–195why, one step back193}194195_fzf_dir_completion() {Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
line 196why, one step back196 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 197–202why, one step back197}198199_fzf_complete_kill() {200 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1201202 local selected fzf[bash] Fix shellcheck warningsJunegunn Choibefore adoption09a2ab3
line 203why, one step back203 [ "${FZF_TMUX:-1}" != 0 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 204–215why, one step back204 tput sc205 selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')206 tput rc207208 if [ -n "$selected" ]; then209 COMPREPLY=( "$selected" )210 return 0211 fi212}213214_fzf_complete_telnet() {215 _fzf_complete '+m' "$@" < <([shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
line 216why, one step back216 command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 217–222why, one step back217 awk '{if (length($2) > 0) {print $2}}' | sort -u218 )219}220221_fzf_complete_ssh() {222 _fzf_complete '+m' "$@" < <([shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
lines 223–225why, one step back223 cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*') \224 <(command grep -oE '^[^ ]+' ~/.ssh/known_hosts | tr ',' '\n' | awk '{ print $1 " " $1 }') \225 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 226–250why, one step back226 awk '{if (length($2) > 0) {print $2}}' | sort -u227 )228}229230_fzf_complete_unset() {231 _fzf_complete '-m' "$@" < <(232 declare -xp | sed 's/=.*//' | sed 's/.* //'233 )234}235236_fzf_complete_export() {237 _fzf_complete '-m' "$@" < <(238 declare -xp | sed 's/=.*//' | sed 's/.* //'239 )240}241242_fzf_complete_unalias() {243 _fzf_complete '-m' "$@" < <(244 alias | sed 's/=.*//' | sed 's/.* //'245 )246}247248# fzf options249complete -o default -F _fzf_opts_completion fzf250Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
lines 251–252why, one step back251d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"252a_cmds="GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 253–255why, one step back253 awk cat diff diff3254 emacs emacsclient ex file ftp g++ gcc gvim head hg java255 javac ld less more mvim nvim patch perl python rubyMake fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
line 256why, one step back256 sed sftp sort source tail tee uniq vi view vim wc xdg-openGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 257–263why, one step back257 basename bunzip2 bzip2 chmod chown curl cp dirname du258 find git grep gunzip gzip hg jar259 ln ls mv open rm rsync scp260 svn tar unzip zip"261x_cmds="kill ssh telnet unset unalias export"262263# Preserve existing completion0.11.3Junegunn Choibefore adoption40d934e
line 264why, one step back264if [ "$_fzf_completion_loaded" != '0.11.3' ]; thenGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
line 265why, one step back265 # Really wish I could use associative array but OSX comes with bash 3.2 :([shell] Suppress alias/function expansionJunegunn Choibefore adoption6081eac
lines 266–267why, one step back266 eval $(complete | command grep '\-F' | command grep -v _fzf_ |267 command grep -E " ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)0.11.3Junegunn Choibefore adoption40d934e
line 268why, one step back268 export _fzf_completion_loaded=0.11.3GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 269–275why, one step back269fi270271if type _completion_loader > /dev/null 2>&1; then272 _fzf_completion_loader=1273fi274275_fzf_defc() {[bash] Update completion.bashJunegunn Choibefore adoptionc3aa836
line 276why, one step back276 local cmd func opts orig_var orig defGO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 277–282why, one step back277 cmd="$1"278 func="$2"279 opts="$3"280 orig_var="_fzf_orig_completion_$cmd"281 orig="${!orig_var}"282 if [ -n "$orig" ]; then[bash] Update completion.bashJunegunn Choibefore adoptionc3aa836
lines 283–284why, one step back283 printf -v def "$orig" "$func"284 eval "$def"GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 285–294why, one step back285 else286 complete -F "$func" $opts "$cmd"287 fi288}289290# Anything291for cmd in $a_cmds; do292 _fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"293done294Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
lines 295–299why, one step back295# Directory296for cmd in $d_cmds; do297 _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"298done299GO15VENDOREXPERIMENT=1 (#430)Junegunn Choibefore adoptiond4e2670
lines 300–313why, one step back300unset _fzf_defc301302# Kill completion303complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill304305# Host completion306complete -F _fzf_complete_ssh -o default -o bashdefault ssh307complete -F _fzf_complete_telnet -o default -o bashdefault telnet308309# Environment variables / Aliases310complete -F _fzf_complete_unset -o default -o bashdefault unset311complete -F _fzf_complete_export -o default -o bashdefault export312complete -F _fzf_complete_unalias -o default -o bashdefault unalias313Make fuzzy completion customizable with _fzf_compgen_{path,dir}Junegunn Choibefore adoption9617647
line 314why, one step back314unset cmd d_cmds a_cmds x_cmds