[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
Provenance
- Commit
d4ad4a2- Tree
8ed6312- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
30577b0
2 files changed+16 added−31 removed
modified:shell/completion.bash+15 added-30 removed
233 unmodified lines234 return 0235 fi236 }237 238-_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-}244-245-_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 }246 254-_fzf_complete_unset() {247+_fzf_var_completion() {248 _fzf_complete '-m' "$@" < <(249 declare -xp | sed 's/=.*//' | sed 's/.* //'250 )251 }252 260-_fzf_complete_export() {253+_fzf_alias_completion() {254 _fzf_complete '-m' "$@" < <(262- declare -xp | sed 's/=.*//' | sed 's/.* //'263- )264-}265-266-_fzf_complete_unalias() {267- _fzf_complete '-m' "$@" < <(255 alias | sed 's/=.*//' | sed 's/.* //'256 )257 }258 9 unmodified lines268 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"285-x_cmds="kill ssh telnet unset unalias export"272+x_cmds="kill"273 274 # Preserve existing completion275 eval "$(complete |276 sed -E '/-F/!d; / _fzf/d; '"/ ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |28 unmodified lines305 __fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o dirnames"306 done307 308 # Kill completion322-complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill309+complete -F _fzf_complete_kill -o default -o bashdefault kill310 324-# Host completion325-complete -F _fzf_complete_ssh -o default -o bashdefault ssh326-complete -F _fzf_complete_telnet -o default -o bashdefault telnet327-328-# Environment variables / Aliases329-complete -F _fzf_complete_unset -o default -o bashdefault unset330-complete -F _fzf_complete_export -o default -o bashdefault export331-complete -F _fzf_complete_unalias -o default -o bashdefault unalias332-311 unset cmd d_cmds a_cmds x_cmds312 313 _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 }332 333+# Environment variables / Aliases / Hosts334+_fzf_setup_completion 'var' export unset335+_fzf_setup_completion 'alias' unalias336+_fzf_setup_completion 'host' ssh telnet337+338 fimodified:README.md+1 added-1 removed
434 unmodified lines435 (`complete | grep _fzf` to see the list). But you can enable it for other436 commands as well by using `_fzf_setup_completion` helper function.437 438 ```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