Skip to content
Myr

[bash-completion] Fix default alias/variable completion

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #1795

authored by Junegunn Choi · committed by Junegunn Choi · parent 30577b0 · Browse files · 2 files+16 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+16 added−31 removed

  • other:+16 added−31 removed

2 files+16 added−31 removed

modified:shell/completion.bash+15 added-30 removed
233 unmodified lines
234 return 0235 fi236}237238_fzf_complete_telnet() {238_fzf_host_completion() {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' "$@" < <(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 '[*?]') \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}246254_fzf_complete_unset() {247_fzf_var_completion() {248 _fzf_complete '-m' "$@" < <(249 declare -xp | sed 's/=.*//' | sed 's/.* //'250 )251}252260_fzf_complete_export() {253_fzf_alias_completion() {254 _fzf_complete '-m' "$@" < <(262 declare -xp | sed 's/=.*//' | sed 's/.* //'263 )264}265266_fzf_complete_unalias() {267 _fzf_complete '-m' "$@" < <(255 alias | sed 's/=.*//' | sed 's/.* //'256 )257}258
9 unmodified lines
268 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"285x_cmds="kill ssh telnet unset unalias export"272x_cmds="kill"273274# Preserve existing completion275eval "$(complete |276 sed -E '/-F/!d; / _fzf/d; '"/ ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |
28 unmodified lines
305 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"306done307308# Kill completion322complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill309complete -F _fzf_complete_kill -o default -o bashdefault kill310324# 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 unalias332311unset cmd d_cmds a_cmds x_cmds312313_fzf_setup_completion() {314 local kind fn cmd315 kind=$1316 fn=_fzf_${1}_completion317 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then340 echo "usage: ${FUNCNAME[0]} path|dir COMMANDS..."318 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host COMMANDS..."319 return 1320 fi321 shift322 for cmd in "$@"; do323 eval "$(complete -p "$cmd" 2> /dev/null | grep -v "$fn" | __fzf_orig_completion_filter)"324 case "$kind" in347 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;348 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;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" ;;329 esac330 done331}332333# Environment variables / Aliases / Hosts334_fzf_setup_completion 'var' export unset335_fzf_setup_completion 'alias' unalias336_fzf_setup_completion 'host' ssh telnet337338fi
modified:README.md+1 added-1 removed
434 unmodified lines
435(`complete | grep _fzf` to see the list). But you can enable it for other436commands as well by using `_fzf_setup_completion` helper function.437438```sh439# usage: _fzf_setup_completion path|dir COMMANDS...439# usage: _fzf_setup_completion path|dir|var|alias|host COMMANDS...440_fzf_setup_completion path ag git kubectl441_fzf_setup_completion dir tree442```443
165 unmodified lines