how can I install oh-my-posh in git bash windows terminal - themes

I have installed it in PowerShell and it worked, how can I install oh-my-posh in git bash?
I Have installed oh-my-posh with scoop then I added this code to .bashrc:
eval "$(oh-my-posh --init --shell bash --config ~/amro.omp.json)"
but unfortunately didn't work

If your .bashrc doesn't load, it's either not sourced (or the right one). A quick search reveals the following issue:
OK, I found out the problem. Quite simply, the bash terminal used by the latest Git for Windows 2.5.0 (mintty) doesn't bother to read .bashrc - it reads .bash_profile. So you can set up your environment in .bash_profile and/or put this code at the start to read .bashrc:
if [ -f ~/.bashrc ]
then
. ~/.bashrc
fi
Secondly, the command needs to be tweaked to this:
eval "$(oh-my-posh init bash --config ~/amro.omp.json)"
Original answer here.

Related

Show Git Branch Name in Terminal - MacOS Catalina - missing zsh files

I have a fresh MacOS Catalina 10.15.6 and I'm following the steps on this link show git branch name in terminal macos so I can display the branch name on my terminal whenever I cd in a git repository.
I'm in my home directory so when I run ls -la, I can't see any of the following files
.zshrc, .bashrc, .bash_profile, .zprofile
The only relevant files showing on the list are .bash_history and .zsh_history.
Are these located on a different path or am I missing any step in the process?
MACOS Instructions- execute the following commands in your terminal:
cd
nano .zshrc
copy and paste this lines of code:
autoload -Uz vcs_info
precmd() {vcs_info}
zstyle ':vcs_info:git:*' formats '(%b)'
setopt PROMPT_SUBST
PROMPT='%n> ${PWD/#$HOME/~} ${vcs_info_msg_0_}%%'
CTRL+o ENTER to save and CTRL+x to exit
source .zshrc
and that's it. Please share your configuration!
Simplest answer, install Oh My ZSH
Trust me, you can't do a better job customizing your own .zshrc file.
Just go here: https://ohmyz.sh/#install
Run this command in your terminal:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Voila, you got pretty terminal outputs
Following this answer on SuperUser StackExchange, I would assume that you have to create .zshrc, .bashrc, etc., on your own. As far as I can tell, you just need a .zshrc for what you are doing. Then you can continue with the setup from the tutorial.

Git - how do I add the configuration for me to "tab" to get the correct branch or branches if there aren't any matches? [duplicate]

E.g. on a fresh ubuntu machine, I've just run sudo apt-get git, and there's no completion when typing e.g. git check[tab].
I didn't find anything on http://git-scm.com/docs, but IIRC completion is included in the git package these days and I just need the right entry in my bashrc.
On Linux
On most distributions, git completion script is installed into /etc/bash_completion.d/ (or /usr/share/bash-completion/completions/git) when you install git, no need to go to github. You just need to use it - add this line to your .bashrc:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
In some versions of Ubuntu, git autocomplete may be broken by default, reinstalling by running this command should fix it:
sudo apt-get install git-core bash-completion
On Mac
You can install git completion using Homebrew or MacPorts.
Homebrew
if $BASH_VERSION > 4: brew install bash-completion#2 (updated version)
Pay special care which version of bash you have as MacOS default ships with 3.2.57(1)-release.
add to .bash_profile:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
For older versions of bash: brew install bash-completion
add to .bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
MacPorts
sudo port install git +bash_completion
then add this to your .bash_profile:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
more info in this guide: Install Bash git completion
Note that in all cases you need to create a new shell (open a new terminal tab/window) for changes to take effect.
i had same issue, followed below steps:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
then add the following lines to your .bash_profile (generally under your home folder)
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
source : http://code-worrier.com/blog/autocomplete-git/
Most of the instructions you see will tell you to download
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
and source that in your bash startup script like .bashrc.
But there is a problem with that, because it is referencing the master branch, which is the latest version of git-completion.bash. The problem is that sometimes it will break because it is not compatible with the version of git you've installed.
In fact, right now that will break because the master branch's git-completion.bash has new features that requires git v2.18, which none of the package managers and installers have updated to yet. You'll get an error unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
So the safest solution is to reference the version/tag that matches the git you've installed. For example:
https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash
Note that it has a v2.17. in the URL instead of master. And then, of course, make sure to source that in the bash startup script.
Ubuntu 14.10
Install git-core and bash-completion
sudo apt-get install -y git-core bash-completion
For current session usage
source /usr/share/bash-completion/completions/git
To have it always on for all sessions
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
Just do this in your ~/.bashrc:
source /usr/share/bash-completion/completions/git
Other answers are telling you to install bash-completion, you don't need to do that, but if you do, then there's no need to source the completion directly. You do one or the other, not both.
A more generic solution is querying the system location as recommended by the bash-completion project:
source "$(pkg-config --variable=completionsdir bash-completion)"/git
See https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
You just need to source the completion script
on my ubuntu there is a file installed here:
source /etc/bash_completion.d/git-prompt
you can follow the links into the /usr/lib/git-core folder. You can find there an instruction, how to set up PS1 or use __git_ps1
macOS via Xcode Developer Tools
Of all the answers currently posted for macOS, this is only mentioned in a very brief comment by jmt...
If you already have the Xcode developer tools installed, then you shouldn't need to download anything new.
Instead, you just need to locate the already-existing git-completion.bash file and source it in your .bashrc. Check the following directories:
/Applications/Xcode.app/Contents/Developer/usr/share/git-core
/Library/Developer/CommandLineTools/usr/share/git-core
Failing that, git itself might be able to help you out. When I run git config as follows, git reports a setting which comes from a gitconfig file located in the same directory as my git-completion.bash:
$ git config --show-origin --list
...
file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig credential.helper=osxkeychain
...
or you can always brute-force search your machine and grab some coffee:
$ find / -type f -name git-completion.bash 2>/dev/null
Thus, I have the following insertion for my ~/.bashrc:
# Git shell completion and prompt string on macOS
_git_dir="/Applications/Xcode.app/Contents/Developer/usr/share/git-core"
if [ -f "${_git_dir}/git-completion.bash" ]; then
source "${_git_dir}/git-completion.bash"
fi
if [ -f "${_git_dir}/git-prompt.sh" ]; then
source "${_git_dir}/git-prompt.sh"
fi
unset _git_dir
Note that this sources the git prompt-string script as well, since it resides in the same directory.
(Tested in macOS Catalina)
May be helpful for someone:--
After downloading the .git-completion.bash from the following link,
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
and trying to use __git_ps1 function, I was getting error as--
-bash: __git_ps1: command not found
Apparently we need to download scripts separately from master to make this command work, as __git_ps1 is defined in git-prompt.sh . So similar to downloading .git-completion.bash , get the git-prompt.sh:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
and then add the following in your .bash_profile
source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi
source ~/.bash.git will execute the downloaded file and
export PS1='\W$(__git_ps1 "[%s]") command will append the checkout out branch name after the current working directory(if its a git repository).
So it will look like:-
dir_Name[branch_name] where dir_Name is the working directory name and branch_name will be the name of the branch you are currently working on.
Please note -- __git_ps1 is case sensitive.
Arch Linux
Source /usr/share/git/completion/git-completion.bash in one of the bash startup files.
For example:
# ~/.bashrc
source /usr/share/git/completion/git-completion.bash
You may be able to find the script in other locations like /usr/share/bash-completion/completions/git but these scripts did not work for me.
Mac M1
For those that are using Mac M1 environment, I was able to install via homebrew:
brew install bash-completion
Then added to my ~/.bash_profile or ~/.bashrc (whatever you use):
[[ -r "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh"
You may need to update the version number (1.3_3). You'll just need to look it up in that directory. I would love to know if there's a better way.
Windows
How it works for me finally on Windows 10 command line (cmd):
Install Clink
Copy git-autocomplete.lua file into C:\Users\<username>\AppData\local\clink directory
Restart Windows
Ubuntu
There is a beautiful answer here. Worked for me on Ubuntu 16.04
Windows
Git Bash is the tool to allow auto-completion. Not sure if this is a part of standard distribution so you can find this link also useful.
By the way, Git Bash allows to use Linux shell commands to work on windows, which is a great thing for people, who have experience in GNU/Linux environment.
On Ubuntu 22.04 just add this line at the end of .bashrc or .zshrc
source /etc/bash_completion.d/git-prompt
On Github in the Git project, They provide a bash file to autocomplete git commands.
You should download it to home directory and you should force bash to run it. It is simply two steps and perfectly explained(step by step) in the following blog post.
code-worrier blog: autocomplete-git/
I have tested it on mac, it should work on other systems too. You can apply same approach to other operating systems.
Just put below in the .bashrc and relaunch the terminal. Navigate to Git repo to see the path in the prompt.
PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] # \[\033[0;36m\]\h \w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] '

How to set `pipenv shell` prompt to match the Terminal's PS1 prompt?

I'm using MacOS and bash.
I'm using pipenv for virtual environments and I really want to make the look of my virtual shell match the look of my standard shell.
When I'm outside of pipenv, I set my .bash_profile so I could have the time, a green command prompt that shows the full path of my working directory and, if that directory has a git repo, it shows my current branch at the end.
I've searched for the correct file to edit to get the same look for each new pipenv shell, but I'm not having any luck finding the right file to copy my profile preferences to.
I'm wondering how I can set it up with my standard PS1 terminal appearance (colors and current git branch, etc).
pipenv-FAQ
Look at the shell does not show the virtualenv’s name in prompt:
This is intentional. You can do it yourself with either shell plugins, or clever PS1 configuration. If you really want it back, use
And for the "shell plugins"
You need to look not for "pipenv+bash" but for virtualenv+bash prompt solutions, such as:
How do I change the default virtualenv prompt?
It's hard for me to recommend "Which way is the best?"
(I'm using zsh myself)
I've searched for the correct file to edit to get the same look for each new pipenv shell, but I'm not having any luck finding the right file to copy my profile preferences to.
For bash, you can create and modify ~/.bashrc.
The pipenv shell command is a wrapper for virtualenv, which checks for a ~/.bashrc file when it activates a virtual environment for a bash shell. Anything in the ~/.bashrc file gets used in the spawned shell, including prompts, aliases, and other things you customized in your shell.
$ cat ~/.bashrc
cat: /Users/gino/.bashrc: No such file or directory
$ pipenv shell
Launching subshell in virtual environment…
bash-5.0$ . /Users/gino/.venvs/test-oiEjhH94/bin/activate
(test) bash-5.0$ ll
bash: ll: command not found
(test) bash-5.0$ exit
$ vim ~/.bashrc
$ cat ~/.bashrc
PS1='\[\e[1;33m\]\u#\W\$\[\e[0m\] '
alias ll="ls -Flh"
$ pipenv shell
Launching subshell in virtual environment…
gino#test$ . /Users/gino.mempin/.venvs/test-oiEjhH94/bin/activate
(test) gino#test$
(test) gino#test$ type ll
ll is aliased to `ls -Flh'
What you can do is to have a ~/.bash_prompt file where you set all your prompt configs (PS1) and a ~/.bash_aliases file where you set all your aliases. Then, source them both in the ~/.bash_profile (for the standard shell) and in the ~/.bashrc (for the pipenv shell).
~/.bash_prompt
# Format the prompt
# Shows up as:
# machine-name#current-working-directory$
PS1='\[\e[1;33m\]\u#\W\$\[\e[0m\] '
# other prompt configs
~/.bash_aliases
alias ll="ls -FlhpG"
# other aliases
~/.bash_profile
# For "main" shell (i.e. Terminal, VS Code terminal)
. ~/.bash_prompt
. ~/.bash_aliases
~/.bashrc
# For `pipenv shell`
. ~/.bash_prompt
. ~/.bash_aliases
A better solution would be to tell pipenv shell or virtualenv to reuse the configs in the ~/.bash_profile, but I haven't got that successfully to work (Terminal, pipenv, VS Code).
macOS 10.15
bash 5.0 (installed via Homebrew)
pipenv 2020.8.13

.bashrc not sourced on Intellij IDEA's terminal

I have some aliased defined on my .bashrc that I'd like to use on my Intellij IDEA's terminal. Why is .bashrc not sourced?
.bashrc is only loaded when Bash is running in "interactive" mode. For some reason, IntelliJ defaults to running Bash in non-interactive mode, but this is easy to change. Open the IntelliJ Settings window, then open "Tools -> Terminal", and add -i to the Shell path.
(Note that in this screenshot, I have also changed the default shell, because I'm on a Mac, which makes it difficult to update /bin/bash. Unless you have installed a different version of Bash, do not blindly copy this change!)
Had the same issue with IntelliJ.
Solved this by setting the value /bin/bash --login in setting->Tools->Terminal->Shell path.
/bin/bash --login forces the bash to read the ~/.bash_profile.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/205437150-Terminal-not-sourcing-bash-profile?page=1#community_comment_360000167679
In your home directory, add theses lines to .profile (create the file if it does not exist), in order to source .bashrc:
if [ "$SHELL" = "/bin/bash" ]; then
. ~/.bashrc
fi
For me, changing the line
Exec="/opt/idea-IU-183.4284.148/bin/idea.sh" %f
to
Exec=bash -ic "/opt/idea-IU-183.4284.148/bin/idea.sh" %f
worked. We exploit the hack that the interactive shell loads .bashrc :)
On Mac OSX Catalina, "/bin/bash" and ~/.zprofile worked for me:
Update:
Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. .bashrc is now .zshrc and .bash_profile is now .zprofile.
My settings are in ~/.bash_profile . I solved it using:
echo "source ~/.bash_profile" >> .profile
sudo echo ". ~/.bashrc" >> /etc/bash.bashrc
Because > operator doesn't pass the sudo permissions to the write process.
I did this :
echo ". ~/.bashrc" | sudo tee /etc/bash.bashrc
I noticed that .bashrc isn't sourced only when I first install IntelliJ and run it directly via the idea.sh script in bin/.
Once I create the desktop entry via Tools -> Create Desktop Entry... and start it from the Ubuntu dash, .bashrc is sourced properly.
If you have recently moved to zsh from bash then go to ~/.zshrc file and update $PATH variable there:
Default value in .zshrc:
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
Change to same PATH variable set in .bashrc and uncomment it
# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:<my-bash-paths>:$PATH
None of these answers worked for me. What did work is
sudo nano /etc/environment
...then manually adding my export and alias commands here to make them system wide.
But be careful, do NOT mess up the PATH or you'll have trouble logging back in to your desktop environment, or many other issues.
I had this problem because the default shell had been changed to zsh.
I echoed the shell name with echo $SHELL to see this (thanks How to get default shell).
Then I changed it back to /bin/bash with this command: chsh -s /bin/bash (thanks https://apple.stackexchange.com/a/100476/176809).
Create a bash script with the content;
echo "source ~/.bash_profile" >> .profile
Then in Intellij go to preferences/tools/Startup Tasks;
create a run configuration that runs your bash script and you're good to go.
I just restarted my MAC and it picked up the new stuff.
For me at least, the only thing that works was to put this in the Shell path variable inside of Tools > Terminal :
/bin/bash --rcfile ~/.bashrc

Homebrew’s `git` not using completion

When using OSX’s git, after I modify a file I can simply do git commit <tab>, and that’ll auto complete the file’s name to the one that was modified. However, if I install a newer version of git from homebrew and I use it, that feature no longer works (meaning I press <tab> and it just “asks” me what file I want to do it on, even including the ones that have no changes).
Can anyone shed some light as to why, and how to solve that? I’d prefer using homebrew’s git, since it’s more up-to-date.
My shell is zsh, and Neither installing bash-completion or zsh-completions worked (even after following homebrew’s post-install instructions).
Also, after installing git with homebrew it says
Bash completion has been installed to: /usr/local/etc/bash_completion.d
zsh completion has been installed to: /usr/local/share/zsh/site-functions
So shouldn’t I be able to use one of those?
You're looking for:
brew install git bash-completion
As warpc's comment states, you'll need to add the following to your ~/.bash_profile to get homebrew's bash-completion working:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
The above is mentioned in the caveats when you install the bash-completion formula.
Note: if you are using Bash v4 or later (via brew install bash) then you're going to want to use brew install bash-completion#2, to enable tab completion add the following to ~/.bash_profile as described in the caveats:
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
The additional export is necessary for git, docker, youtube-dl, and other completions which may be included in the $(brew --prefix)/etc/bash_completion.d/ directory.
This get's git tab completion working on OSX without having to restart your terminal:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
echo "source ~/.git-completion.bash" >> ~/.bash_profile
source ~/.bash_profile
EDIT: this doesn't work in Catalina's default zsh shell. I changed the default shell back to bash and it works again. https://www.howtogeek.com/444596/how-to-change-the-default-shell-to-bash-in-macos-catalina/
In case anyone else makes my dumb mistake, try brew install git. I was using the git that comes with Xcode and didn't realize that I had never installed Homebrew's git to get the autocompletions.
for some reason I was missing the file at $(brew --prefix)/etc/bash_completion so #Graham Perks' correct answer didn't work for me
It ended up the fix in my case was:
brew unlink bash-completion
brew link bash-completion
I solved the problem by figuring out that $(brew --prefix)/etc/bash_completion returned Permission denied when executed. So after a simple:
chmod +x $(brew --prefix)/etc/bash_completion
Everything is now working fine. I'm wondering why Homebrew doesn't make the bash_completion file executable on installation, though.
For bash on macOS Catalina (3/30 update: Big Sur too), if you want to also use Bash 5 from homebrew, you need to make sure that your login shell is set to homebrew's bash, and not the default.
To check if you need to do this, run echo ${BASH_VERSION}. If you see a version starting with 3, you are not using Brew's bash for your login shell.
To change this,
Open System Preferences->Users and Groups.
Right click your user and select "Advanced Options". You may need to unlock this with your password by clicking the lock in the bottom left.
Set the login shell field to the location of your brew's bash, which you can usually find by running which bash in a terminal after you install brew's bash. Mine was /usr/local/bin/bash.
Restart your terminal, and follow the instructions in this excellent answer
Found a working solution. It's very recent (authored 16 hours ago, and committed 2 hours ago), and it comes directly from homebrew.
brew install git --without-completions
Just tried it, and it finally works as intended.
I had the same issue and even found this post this morning. I fixed the issue by updating brew with brew update and then reinstalling git with brew reinstall git.
I was then notified of another file that is blocking the homebrew linking process, in my case it was /usr/local/share/zsh/site-functions/git-completion.bash. Removing the file and running brew link git solved the issue. Guessing it was just a bad recipe version we stumbled upon.
If you have $BASH_VERSION < 4.1, eg 3.2.57(1)-release then go ahead with:
brew install bash-completion
# In ~/.bash_profile :
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
However if you've brew install bash to get version 4.4.12(1)-release
you can use the better and more complete completions in:
brew install bash-completion#2
# In ~/.bash_profile:
[ -f "$(brew --prefix)/share/bash-completion/bash_completion" ] \
&& . "$(brew --prefix)/share/bash-completion/bash_completion"
Note that some packages (brew, docker, tmux) will still put some completions into $(brew --prefix)/etc/bash_completion.d/ so you might add:
for completion in "$(brew --prefix)/etc/bash_completion.d/"*
do
. $completion
done
Finally you should be able to add the git completion script if for some reason the way you installed git did not add it to either of those:
[[ -f $(brew --prefix)/etc/bash_completion.d/git \
|| -f $(brew --prefix)/share/bash-completion/completions/git ]] \
|| curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash \
-o $(brew --prefix)/etc/bash_completion.d/git
You can get and add it with the above.
Step 1: Download auto completion script:
cd ~
curl -O https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
Step 2: Update .bash_profile and .bashrc
echo "source ~/git-completion.bash" >> .bash_profile
Via https://www.anintegratedworld.com/git-tab-autocomplete-on-osx-10-11-el-capitan/
If above does not work, try https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion
In 2019, using Bash v5, you do not need to explicitly source the git bash completion script in your .bash_profile or .bashrc
Ensure you have the following two lines in your .bashrc
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
Download the git bash completion script (https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash) and save it to /usr/local/etc/bash_completion.d/ as git
That's it! Bash will automatically pick up the git completion file and enable completion.
Side Note: I recommend putting all these changes in .bashrc as this ensures that when you drop into an interactive shell (ie. from pipenv shell), completions will get loaded correctly as bash will source .bashrc and NOT .bash_profile.
For me , I had to put
source $(brew --prefix)/etc/bash_completion
into .bashrc (not .bash_profile) to get this to work.
".bash_profile is executed for login shells, while .bashrc is executed
for interactive non-login shells" -- from What is the difference between .bash_profile and .bashrc? It appears to me that MacOS Sierra doesn't execute .bash_profile when opening a new terminal window, only .bashrc.
I wouldn't put it in _bash_profile, because then I'd have to reboot/logout for updates to take effect.
This worked for me in Mojave (OSX 10.14.1):
brew install bash-completion
Then add the following line to your ~/.bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
It may have something to do with libedit being used instead of readline in Lion.
Try installing readline before git.
brew install readline
For those who already have brew bash-completion installed. I did not have the git completion script installed and could not find any tap for that.
So I added it manually:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o $(brew --prefix)/etc/bash_completion.d/git
Note that you have to rename the file and remove the extension for it to work.
If you do not have completion or git installed, install it in the accepted answer.
brew install git bash-completion
If you used homebrew to install git, then probably there is no need to install anything to support git completion.
"git-completion.bash" file is somewhere (mine was here: /usr/local/git/contrib/completion/git-completion.bash)
All you need to do is to find the file:
sudo find / -type f -name "git-completion.bash"
Then source its path in your .bash_profile.
For example I needed to add this line to my ~/.bash_profile:
source /usr/local/git/contrib/completion/git-completion.bash
Don't forget to source your ~/.bash_profile or reopen your terminal ;)
from:
how-enable-git-tab-completion-bash-mac-os-x
I know this is an old post, but you don't really need to install any additional packages.
Homebrew is informing you that there is a directory with all the stuff you need.
You can simply add the following line to your .bash_profile if you are using Bash:
source /usr/local/etc/bash_completion.d/git-completion.bash
If nothing works, it could be because you have an older version of bash and bash completion script is not getting sourced by the /usr/local/etc/profile.d/bash_completion.sh script. You can test this by adding a simple echo inside the conditionals in file /usr/local/etc/profile.d/bash_completion.sh:
10 if shopt -q progcomp && [ -r /usr/local/Cellar/bash-completion#2/2.11/share/bash-completion/bash_completion ]; then
11 # Source completion code.
echo "doing bash completion or not"
12 . /usr/local/Cellar/bash-completion#2/2.11/share/bash-completion/bash_completion
And open a new terminal. If you don't see the echo message, then the conditionals do not evaluate to true. In my case it was because the bash version was old, the default from mac 3.2.blah.
I did install a newer bash from brew, but i forgot to chsh and that caused me a lot of headache. bash --version would return 5.1.8 but the enabled shell was still the old one :) To test the enabled bash you can do
for n in {0..5}
do
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
done
The fix was to sudo chsh -s /usr/local/bin/bash After which the completions worked.
After tearing my hair out over this one for ages, I discovered that when I had the hub command installed, the completions for the hub command were breaking the completions for git. I had to remove /usr/local/etc/bash_completion.d/hub.bash_completion.sh. This meant no completions for hub, but git completions now worked. I didn't debug why this was.
I had the following brew packages installed:
bash: 5.1.16
bash-completion#2: 2.11
git: 2.35.1
hub: 2.14.2
Enable Auto Completion of GIT commands on MAC-OS Mojave 10.14
I am a developer and use GIT from the command line all of the time. When I consider the development perspective, I used to execute a lot of commands using the command line for GIT operations. Most of the time it is very annoying that MAC OS doesn't have automatic support for the command completion which I partially support. as well as the command suggestions, which means what are the commands available for typed characters. So it is very troublesome to type very long command and mostly repetitive task as typo going wrong. :(
Tab completion would certainly be faster and easier. Unfortunately, the default installation of git on some Mac computers doesn't have tab completion enabled.
So that I was searching for a fix for the problem and there are several solutions found from the web search such as StackOverflow, GitHub as well as from the medium. Unfortunately, those solutions did not work for me and got frustrated with trying different solutions so many times.
I was searching deeply and trying out different solutions and fortunately, it is an easy fix. Below are the steps I have collected from several posts and finally it worked as expected. So I hope to share with others who have this problem like me.
f you go to the web search and you can find many solutions which mentioned the git completion bash file. Even GitHub guide as well. But I suggest you check first if the git-completion.bash file is already in your MAC computer with the git-core or something else which came from installation. you can use below command.
sudo find / -type f -name "git-completion.bash"
you will get below results. (may have some difference according to the content)
/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
/Users/Dilanka/git-completion.bash
/Users/Dilanka/.oh-my-zsh/plugins/gitfast/git-completion.bash
/Users/Dilanka/Downloads/git-completion.bash
I suggest you to pick which installed from git-core
If the git-completion.bash script doesn't exist on your machine, please retrieve it from the below provided above and save it to your local machine in a new file called git-completion.bash in the /usr/local/etc/bash_completion.d/ directory.
https://git-scm.com/book/en/v1/Git-Basics-Tips-and-Tricks
If you use the Bash shell, Git comes with a nice auto-completion script you can enable. Download it directly from the Git source code at
https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
If the git-completion.bash script exists on your machine, but is not in the /usr/local/etc/bash_completion.d/ directory, you should create that directory and copy the file into it. Below command will do the job:
sudo mkdir /opt/local/etc/bash_completion.d
sudo cp /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash /usr/local/etc/bash_completion.d/git-completion.bash
After the completion of above. The git-completion.bash script should exist on your local machine in the/usr/local/etc/bash_completion.d/ directory.
Now you need to refresh your profile using below command. It will load your added bash file to the terminal context.
source ~/.bash_profile
Great!!! you have done it. Just start the terminal window and try it. Just type "git sta" it will show suggestions as below:
git sta
stage stash status
git chec<TAB> will show git checkout
see my GitHub post here:
https://github.com/DIL8654/Enable-Auto-Completion-of-GIT-commads-on-MAC-OS-Mojave
See my Medium post here:
https://medium.com/#dilanka85/enable-auto-completion-of-git-commands-on-mac-os-mojave-10-14

Resources