Skip to content

Instantly share code, notes, and snippets.

@mrkschan
Last active October 9, 2015 14:27
Show Gist options
  • Save mrkschan/3522031 to your computer and use it in GitHub Desktop.
Save mrkschan/3522031 to your computer and use it in GitHub Desktop.
bash completion for git, with files and directory
#!bash
#
# Enable file/directory completion for git.
#
# bashref - http://is.gd/PHQRX1
# bashlib - http://code.google.com/p/bash-completion-lib/
have git && {
_git_completion()
{
local cur words cword prev
_get_comp_words_by_ref -n =: cur words cword prev
local command subcommand
command=${words[1]}
subcommand=${words[2]}
# Complete with files
if [[ "$command" == "diff" && "$prev" == "--" ]]; then
_filedir
return 0
fi
if [[ "$command" == "checkout" && "$prev" == "--" ]]; then
_filedir
return 0
fi
if [[ "$command" == "log" && "$prev" == "--" ]]; then
_filedir
return 0
fi
if [[ "$command" == "grep" && "$prev" == "--" ]]; then
_filedir
return 0
fi
if [[ "$command" == "add" ]]; then
_filedir
return 0
fi
if [[ "$command" == "blame" ]]; then
_filedir
return 0
fi
if [[ "$command" == "rm" ]]; then
_filedir
return 0
fi
if [[ "$command" == "mv" ]]; then
_filedir
return 0
fi
# Fallback to use git default completion
_git
}
complete -o bashdefault -o nospace -F _git_completion git
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment