Skip to content
Myr

completion.bash

This file

Path

shell/completion.bash

332 lines · 8.9 KiB · Shell

Last changed by

[completion] Filter out non-hostnames in SSH config file (#1329)Jaybefore adoption6de1ad9

History of this file

Take it

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

0.16.9Junegunn Choibefore adoption18a1aea

lines 1–39why, 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)1112# To use custom commands instead of find, override _fzf_compgen_{path,dir}13if ! declare -f _fzf_compgen_path > /dev/null; then14 _fzf_compgen_path() {15 echo "$1"16 command find -L "$1" \17 -name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \18 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'19 }20fi2122if ! declare -f _fzf_compgen_dir > /dev/null; then23 _fzf_compgen_dir() {24 command find -L "$1" \25 -name .git -prune -o -name .svn -prune -o -type d \26 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'27 }28fi2930###########################################################3132# To redraw line after fzf closes (printf '\e[5n')33bind '"\e[0n": redraw-current-line'3435__fzfcmd_complete() {36 [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] &&37 echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"38}39

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

lines 40–41why, one step back
40__fzf_orig_completion_filter() {
41 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 42why, one step back
42 awk -F= '{OFS = FS} {gsub(/[^A-Za-z0-9_= ;]/, "_", $1);}1'

0.16.9Junegunn Choibefore adoption18a1aea

lines 43–114why, one step back
43}
4445_fzf_opts_completion() {46 local cur prev opts47 COMPREPLY=()48 cur="${COMP_WORDS[COMP_CWORD]}"49 prev="${COMP_WORDS[COMP_CWORD-1]}"50 opts="51 -x --extended52 -e --exact53 --algo54 -i +i55 -n --nth56 --with-nth57 -d --delimiter58 +s --no-sort59 --tac60 --tiebreak61 -m --multi62 --no-mouse63 --bind64 --cycle65 --no-hscroll66 --jump-labels67 --height68 --literal69 --reverse70 --margin71 --inline-info72 --prompt73 --header74 --header-lines75 --ansi76 --tabstop77 --color78 --no-bold79 --history80 --history-size81 --preview82 --preview-window83 -q --query84 -1 --select-185 -0 --exit-086 -f --filter87 --print-query88 --expect89 --sync"9091 case "${prev}" in92 --tiebreak)93 COMPREPLY=( $(compgen -W "length begin end index" -- "$cur") )94 return 095 ;;96 --color)97 COMPREPLY=( $(compgen -W "dark light 16 bw" -- "$cur") )98 return 099 ;;100 --history)101 COMPREPLY=()102 return 0103 ;;104 esac105106 if [[ "$cur" =~ ^-|\+ ]]; then107 COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )108 return 0109 fi110111 return 0112}113114_fzf_handle_dynamic_completion() {

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

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

0.16.9Junegunn Choibefore adoption18a1aea

lines 116–123why, one step back
116 cmd="$1"
117 shift118 orig_cmd="$1"119 orig_var="_fzf_orig_completion_$cmd"120 orig="${!orig_var##*#}"121 if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then122 $orig "$@"123 elif [ -n "$_fzf_completion_loader" ]; then

[bash] Do not print error when falling back to default completion (#1279)ptzzbefore adoption1fb0fbc

line 124why, one step back
124 orig_complete=$(complete -p "$cmd" 2> /dev/null)

0.16.9Junegunn Choibefore adoption18a1aea

lines 125–126why, one step back
125 _completion_loader "$@"
126 ret=$?

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

line 127why, one step back
127 # _completion_loader may not have updated completion for the command

[bash] Do not print error when falling back to default completion (#1279)ptzzbefore adoption1fb0fbc

line 128why, one step back
128 if [ "$(complete -p "$cmd" 2> /dev/null)" != "$orig_complete" ]; then

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

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

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

line 135why, one step back
135 fi

0.16.9Junegunn Choibefore adoption18a1aea

lines 136–150why, one step back
136 return $ret
137 fi138}139140__fzf_generic_path_completion() {141 local cur base dir leftover matches trigger cmd fzf142 fzf="$(__fzfcmd_complete)"143 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"144 COMPREPLY=()145 trigger=${FZF_COMPLETION_TRIGGER-'**'}146 cur="${COMP_WORDS[COMP_CWORD]}"147 if [[ "$cur" == *"$trigger" ]]; then148 base=${cur:0:${#cur}-${#trigger}}149 eval "base=$base"150

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

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

0.16.9Junegunn Choibefore adoption18a1aea

lines 152–160why, one step back
152 while true; do
153 if [ -z "$dir" ] || [ -d "$dir" ]; then154 leftover=${base/#"$dir"}155 leftover=${leftover/#\/}156 [ -z "$dir" ] && dir='.'157 [ "$dir" != "/" ] && dir="${dir/%\//}"158 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; do159 printf "%q$3 " "$item"160 done)

Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9

line 161why, one step back
161 matches=${matches% }

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

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

0.16.9Junegunn Choibefore adoption18a1aea

lines 163–208why, one step back
163 if [ -n "$matches" ]; then
164 COMPREPLY=( "$matches" )165 else166 COMPREPLY=( "$cur" )167 fi168 printf '\e[5n'169 return 0170 fi171 dir=$(dirname "$dir")172 [[ "$dir" =~ /$ ]] || dir="$dir"/173 done174 else175 shift176 shift177 shift178 _fzf_handle_dynamic_completion "$cmd" "$@"179 fi180}181182_fzf_complete() {183 local cur selected trigger cmd fzf post184 post="$(caller 0 | awk '{print $2}')_post"185 type -t "$post" > /dev/null 2>&1 || post=cat186 fzf="$(__fzfcmd_complete)"187188 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"189 trigger=${FZF_COMPLETION_TRIGGER-'**'}190 cur="${COMP_WORDS[COMP_CWORD]}"191 if [[ "$cur" == *"$trigger" ]]; then192 cur=${cur:0:${#cur}-${#trigger}}193194 selected=$(cat | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $1 -q "$cur" | $post | tr '\n' ' ')195 selected=${selected% } # Strip trailing space not to repeat "-o nospace"196 printf '\e[5n'197198 if [ -n "$selected" ]; then199 COMPREPLY=("$selected")200 return 0201 fi202 else203 shift204 _fzf_handle_dynamic_completion "$cmd" "$@"205 fi206}207208_fzf_path_completion() {

Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9

line 209why, one step back
209 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"

0.16.9Junegunn Choibefore adoption18a1aea

lines 210–217why, one step back
210}
211212# Deprecated. No file only completion.213_fzf_file_completion() {214 _fzf_path_completion "$@"215}216217_fzf_dir_completion() {

Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9

line 218why, one step back
218 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"

0.16.9Junegunn Choibefore adoption18a1aea

lines 219–225why, one step back
219}
220221_fzf_complete_kill() {222 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1223224 local selected fzf225 fzf="$(__fzfcmd_complete)"

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

line 226why, one step back
226 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' ' ')

0.16.9Junegunn Choibefore adoption18a1aea

lines 227–243why, one step back
227 printf '\e[5n'
228229 if [ -n "$selected" ]; then230 COMPREPLY=( "$selected" )231 return 0232 fi233}234235_fzf_complete_telnet() {236 _fzf_complete '+m' "$@" < <(237 command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |238 awk '{if (length($2) > 0) {print $2}}' | sort -u239 )240}241242_fzf_complete_ssh() {243 _fzf_complete '+m' "$@" < <(

[completion] Filter out non-hostnames in SSH config file (#1329)Jaybefore adoption6de1ad9

line 244why, one step back
244 cat <(cat ~/.ssh/config /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 245why, one step back
245 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \

0.16.9Junegunn Choibefore adoption18a1aea

lines 246–284why, one step back
246 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |
247 awk '{if (length($2) > 0) {print $2}}' | sort -u248 )249}250251_fzf_complete_unset() {252 _fzf_complete '-m' "$@" < <(253 declare -xp | sed 's/=.*//' | sed 's/.* //'254 )255}256257_fzf_complete_export() {258 _fzf_complete '-m' "$@" < <(259 declare -xp | sed 's/=.*//' | sed 's/.* //'260 )261}262263_fzf_complete_unalias() {264 _fzf_complete '-m' "$@" < <(265 alias | sed 's/=.*//' | sed 's/.* //'266 )267}268269# fzf options270complete -o default -F _fzf_opts_completion fzf271272d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"273a_cmds="274 awk cat diff diff3275 emacs emacsclient ex file ftp g++ gcc gvim head hg java276 javac ld less more mvim nvim patch perl python ruby277 sed sftp sort source tail tee uniq vi view vim wc xdg-open278 basename bunzip2 bzip2 chmod chown curl cp dirname du279 find git grep gunzip gzip hg jar280 ln ls mv open rm rsync scp281 svn tar unzip zip"282x_cmds="kill ssh telnet unset unalias export"283284# Preserve existing completion

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

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

0.16.9Junegunn Choibefore adoption18a1aea

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

0.16.9Junegunn Choibefore adoption18a1aea

lines 288–309why, one step back
288
289if type _completion_loader > /dev/null 2>&1; then290 _fzf_completion_loader=1291fi292293_fzf_defc() {294 local cmd func opts orig_var orig def295 cmd="$1"296 func="$2"297 opts="$3"298 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"299 orig="${!orig_var}"300 if [ -n "$orig" ]; then301 printf -v def "$orig" "$func"302 eval "$def"303 else304 complete -F "$func" $opts "$cmd"305 fi306}307308# Anything309for cmd in $a_cmds; do

Revert "[bash] Do not append space when path completion is cancelled"Junegunn Choibefore adoption9d2c6a9

line 310why, one step back
310 _fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"

0.16.9Junegunn Choibefore adoption18a1aea

lines 311–332why, one step back
311done
312313# Directory314for cmd in $d_cmds; do315 _fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"316done317318unset _fzf_defc319320# Kill completion321complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill322323# Host completion324complete -F _fzf_complete_ssh -o default -o bashdefault ssh325complete -F _fzf_complete_telnet -o default -o bashdefault telnet326327# Environment variables / Aliases328complete -F _fzf_complete_unset -o default -o bashdefault unset329complete -F _fzf_complete_export -o default -o bashdefault export330complete -F _fzf_complete_unalias -o default -o bashdefault unalias331332unset cmd d_cmds a_cmds x_cmds