Completion for git commands
-
29-09-2020 - |
Question
I recently switched from using an Ubuntu box for the last 7 years to a Mac. I have read other posts like Switching from Ubuntu to OS X and I'm lost and What is the difference between iTerm2 and Terminal?
Why don't iTerm2 and Terminal complete the arguments for a command?
Eg. If I do gi
+Tab, it autocompletes this word to git
which is good. When I add a space and try remo
+Tab, why don't I get it autocompleted to remote
like in Ubuntu. I have to type everything manually after the word git
. Same for other commands.
Am I doing something wrong, or missing something?
(Please note that I am not talking about the cmd + ; shortcut which autocompletes previously typed entries in iTerm2.)
Solution
The easiest way to get autocompletion on OS X is to switch your shell to zsh
.
You can test things by running zsh
in iTerm and terminal before making the switch above. If you really want to stick with bash
you can start with these two questions:
OTHER TIPS
A solution when switching from ubuntu is to use homebrew, a package manager for Mac OSX. In particular, for git
, you will have included a number of additions including bash completion. This will be true for other programs which are faliliar for developpers, like make
.
In 2 steps:
- install with
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
- Run
brew doctor
to get your path set up properly - Install git and bash completion:
brew install git bash-completion
- Add the command return in the previous step in your
.bash_profile
. - Restart your bash sessions once the new tools and configuration are written to your file system.
I have two suggestions:
use fish, it's really simple and easy
a. extend it by using oh-my-fish
zsh with oh-my-zsh. zsh is already installed so you don't need to do much.
a.
chsh -s /bin/zsh
b. logout and login again
c. execute this to install oh-my-zsh
wget --no-check-certificate https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh
more info here: https://github.com/robbyrussell/oh-my-zsh
I use oh-my-zsh
but fish
is great as well.
This somewhat sidesteps the problem, in case someone wants to keep the Apple-provided version of Git on their system.
You can create aliases in the .gitconfig file in your home directory for shorter git commands. For example, I have git set up so git st
runs git status
instead.
Example aliases in .gitconfig:
[alias]
co = checkout
ci = commit
st = status
br = branch
One option that might help is to use .git-completion.bash
for bash git autocompletion.
It lets you type git commands and then have autocomplete for the remote name (e.g. 'origin' or 'heroku') and then (and more significantly) for the repository branch names.
See https://apple.stackexchange.com/a/55886/24565
Your download the file from
https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
For those looking for a command completion solution for bash that's as easy to use as zsh + oh-my-zsh, there's now the bash-it project. Which, in its own words, is "a shameless ripoff of oh-my-zsh" -- nothing wrong with that really!
It sets up a plugin like architecture like oh-my-zsh does, where you can bring things in and out of your environment easily and get new things via sync'ing to their github project.
Installation needs three steps:
- Check a clone of this repo:
git clone https://github.com/revans/bash-it.git ~/.bash_it
- Run
~/.bash_it/install.sh
(it automatically backs up your~/.bash_profile
) - Edit your
~/.bash_profile
file in order to customize bash-it.
If you're afraid of a full-on switch to zsh
, bash-it
is a nice middle ground that keeps you on bash
but gives you a lot of powerful plugins to extend your environment with.