diff --git a/.bash_completion b/.bash_completion new file mode 100644 index 0000000..bb319e9 --- /dev/null +++ b/.bash_completion @@ -0,0 +1,7 @@ +# ~/.bash_completion - Custom bash auto completions + +# Enable bash completion for dotfiles manager (if installed) +if command -v dotfiles &>/dev/null; then + source <(dotfiles bash-completion) +fi + diff --git a/bin/dotfiles b/bin/dotfiles index 723884c..2d3ca09 100755 --- a/bin/dotfiles +++ b/bin/dotfiles @@ -2,7 +2,7 @@ ## ## binaryDiv's dotfiles management script -## Version 0.2.0 +## Version 0.3.0 ## usage() { @@ -125,6 +125,46 @@ case "$cmd" in git -C "$dotfilesdir" pull "$@" ;; + bash-completion) + cat <<- 'END_OF_BASHCOMPLETION' + _dotfiles_filenames() { + compopt -o filenames + files=($(compgen -f -X '*/@(.|..|.git)' .dotfiles/$cur)) + COMPREPLY=(${files[@]#.dotfiles/}) + } + _dotfiles() { + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + local commands="help install ls diff link git pull" + local gitcommands="add commit diff fetch log pull push rebase status" + + if [[ $COMP_CWORD -eq 1 ]]; then + COMPREPLY=($(compgen -W "$commands" -- $cur)) + else + case "${COMP_WORDS[1]}" in + ls|diff) + [[ $COMP_CWORD -eq 2 ]] && _dotfiles_filenames + ;; + + link) + _dotfiles_filenames + ;; + + git) + if [[ $COMP_CWORD -eq 2 ]]; then + COMPREPLY=($(compgen -W "$gitcommands" -- ${cur})) + else + _dotfiles_filenames + fi + ;; + esac + fi + } + + complete -F _dotfiles dotfiles + END_OF_BASHCOMPLETION + ;; + *) echo "Unknown command '$cmd', use 'help' to show all commands." exit 1