Improve bash completion

- kill completion: do not even start fzf on non-empty word
- host completion: start fzf with initial query
This commit is contained in:
Junegunn Choi 2013-11-29 23:42:00 +09:00
parent d3742782f3
commit df663c4e41

View File

@ -31,11 +31,10 @@ _fzf_opts_completion() {
} }
_fzf_generic_completion() { _fzf_generic_completion() {
local cur prev opts base dir leftover matches local cur base dir leftover matches
COMPREPLY=() COMPREPLY=()
FZF_COMPLETION_TRIGGER=${FZF_COMPLETION_TRIGGER:-**} FZF_COMPLETION_TRIGGER=${FZF_COMPLETION_TRIGGER:-**}
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${cur} == *"$FZF_COMPLETION_TRIGGER" ]]; then if [[ ${cur} == *"$FZF_COMPLETION_TRIGGER" ]]; then
base=${cur:0:${#cur}-${#FZF_COMPLETION_TRIGGER}} base=${cur:0:${#cur}-${#FZF_COMPLETION_TRIGGER}}
eval base=$base eval base=$base
@ -84,6 +83,8 @@ _fzf_dir_completion() {
} }
_fzf_kill_completion() { _fzf_kill_completion() {
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
local selected local selected
tput sc tput sc
selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ') selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
@ -96,17 +97,17 @@ _fzf_kill_completion() {
} }
_fzf_host_completion() { _fzf_host_completion() {
if [ "${COMP_WORDS[COMP_CWORD-1]}" = '-l' ]; then local cur prev selected
return 1 cur="${COMP_WORDS[COMP_CWORD]}"
fi prev="${COMP_WORDS[COMP_CWORD-1]}"
[ "$cur" = '-l' -o "$prev" = '-l' ] && return 1
local selected
tput sc tput sc
selected=$(grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS) selected=$(grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
tput rc tput rc
if [ -n "$selected" ]; then if [ -n "$selected" ]; then
COMPREPLY=( "$selected" ) COMPREPLY=("$selected")
return 0 return 0
fi fi
} }