dotfiles/dotlib/post/tab.sh

39 lines
869 B
Bash

tab-ssh-hosts() {
mapfile -t hosts < <(awk 'tolower($1) == "host" { print $2 }' ~/.ssh/config)
mapfile -t COMPREPLY < <(compgen -W "${hosts[*]}" -- "${COMP_WORDS[COMP_CWORD]}")
return 0
}
if [[ -f ~/.ssh/config ]]; then
complete -o default -F tab-ssh-hosts ssh scp ssh-copy-id sftp
fi
tab-make-targets() {
local arg prev file targets
if [[ -f makefile ]]; then
file=makefile
elif [[ -f Makefile ]]; then
file=Makefile
fi
for arg in "${COMP_WORDS[@]}"; do
if [[ $prev = -f ]]; then
file=$arg
elif [[ $arg = -f* ]]; then
file=${arg#-f}
fi
prev=$arg
done
mapfile -t targets < <(
awk '$1 ~ /:$/ && $1 !~ /^\./ { gsub(/:$/, "", $1); print $1 }' "$file"
)
mapfile -t COMPREPLY < <(
compgen -W "${targets[*]}" -- "${COMP_WORDS[COMP_CWORD]}"
)
}
complete -o default -F tab-make-targets make gmake