Git autocompletion and autocorrect.
April 30, 2014
Estimated reading time 2 minutes
At work I use Git alot. Most of the time I type too fast and my screen produces jibrish, commonly known as typos. To prevent this frmo happening, there is a fix.
Git itself has a bunch of configuration options to customise behaviour, preferences and visual appearance.
The man git
tells us;
help.autocorrect
This option is available only in Git 1.6.1 and later. If you mistype a command in Git, it shows you something like this:
$ git com
git: 'com' is not a git-command. See 'git --help'.
Did you mean this?
commit
If you set help.autocorrect to 1, Git will automatically run the command if it has only one match under this scenario.
git config --global help.autocorrect 1
The final parameter to that command is the amount of seconds, unline what the manual states. Setting it to 15 means 1.5 seconds, setting it to 1 means 0.1 seconds.
Git Autocompletion.
Open up Terminal and type in the following;
curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion
This downloads the git-completion script and places it in your home folder.
Open up ~/.bash_profile
with your favorite editor and add the following line at the end of it.
source ~/.git-completion
You are now good to go. No open a new Terminal tab or force this command by ‘refreshing’ the terminal by typing source ~/.bash_profile
Still in Terminal type git com
and press the tab key. This should either autocomplete the command or if there are more commands starting with ‘com’ show you a list of possible options.
Enjoy! No moer typos in Git.
[UPDATE] You might want to install ‘Oh My ZSH’
Oh-My-Zsh is an open source, community-driven framework for managing your ZSH configuration. It comes bundled with a ton of helpful functions, helpers, plugins, themes, and a few things that make you shout… “Oh My ZSH!”