Unknown error in Bash [duplicate] - bash

I'm trying to change my command promt in terminal. I keep getting the error:
-bash: __git_ps1: command not found
I've tried it just by typing it into the terminal as is: __git_ps1. I've also tried it out in the .bash_profile
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
As you might be able to see/tell, yes, I do have the auto-completion installed and it does work great!
I came across this question: " PS1 env variable does not work on mac " which gives the code
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
So I add it to my .bash_profile hoping that it will change something. Well, it did. It just changed the error output.
Here's the .bash_profile with the addition:
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
And now here's the changed error output:
sed: (%s): No such file or directory
Note: I've also moved the alias below the source with no difference. I have git version 1.7.12.1
This should be a simple change. Can someone please help me?
Edit 10/13/12
No, I definitely do not want to define __git_ps1 myself but was just trying to see if it would be recognized by doing so. Yes, I have the .git-completion.bash file installed. Here's how I got auto completion on my machine.
cd ~
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git.completion.bash ~/.git-completion.bash
A ls -la then lists the .git-completion.bash file.
Edit 10/13/12 - Solved by Mark Longair (below)
The following code worked for me in the .bash_profile while others did not...
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='Geoff[\W]$(__git_ps1 "(%s)"): '
fi

You've installed the version of git-completion.bash from master - in git's development history this is after a commit that split out the __git_ps1 function from the completion functionality into a new file (git-prompt.sh). The commit that introduced this change, which explains the rationale, is af31a456.
I would still suggest that you just source the version of git-completion.bash (or git-prompt.sh) that is bundled with your installation of git.
However, if for some reason you still want to use this functionality by using scripts separately downloaded from master, you should download git-prompt.sh similarly:
curl -o ~/.git-prompt.sh \
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
... and add the following line to your ~/.bash_profile:
source ~/.git-prompt.sh
Then your PS1 variable that includes __git_ps1 '%s' should work fine.

After upgrading to OSX 10.9 Mavericks I had to reference the following files to get git shell command completion and git prompt to work again.
From my .bash_profile or similar:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
fi
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
#shell prompt example
PS1='\u $(__git_ps1 "(%s)")\$ '

You should
$ brew install bash bash-completion git
Then source "$(brew --prefix)/etc/bash_completion" in your .bashrc.

Following worked for me like a charm:
Run following in your Terminal:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
Open/Create bash_profile:
$ vi ~/.bash_profile
Add following to the file:
source ~/.bash_git
export PS1='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[00m\] '
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
Finally, source it using:
$ source ~/.bash_profile
This will solve the problem of bash: __git_ps1: command not found.
Also your prompt will change to "os ". To change "os" to something else, modify "os" string in export PS1 line.

Solution for MacOS Sierra and git version 2.10.1 <2017-2-06>
Step 1: Install the Git
You can skip this step if you already installed the latest git.
Download git package from browser https://git-scm.com/download/
Note: if you install with curl [option] https://... option to download, you would have to make sure your system support SSL. So for new comer, to download from browser and install directly from git installer is much easier.
Installation Verification:
Show where is your git directory at: which git
Show which version your git currently is: git --version current version should be 2.10.1.
Step 2: Add your git profile to your shell
Open your shell profile:
nano ~/.bash_profile or nano ~/.bashrc Depends on where your modification is.
Add the following code to the file:
source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
Note: git installation location changed from opt/ directory to usr/local/ after OSX upgrade to El Capitain, and this is why some of the old answer above doesn't work anymore in MacOS Sierra.
Add the following code to your PS1 configuration:
Option 1: add directly to your PS1: export PS1='\w$(__git_ps1 "(%s)") > '
I prefer this simple approach since I already know the .git-completion.bash is there in my home directory, and I can add other prompt format in the front of it. here is my personal prompt for your reference: export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
Option 2: Add a selection script
if [ -f ~/.git-completion.bash ]; then
export PS1='\w$(__git_ps1 "(%s)") > '
fi
Save and use the profile: source ~/.bash_profile or source ~/.bashrc
Now you should see the git prompt working properly and shows which branch you are in right now.

High Sierra clean solution with colors !
No downloads. No brew. No Xcode
Just add it to your ~/.bashrc or ~/.bash_profile
export CLICOLOR=1
[ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh ] && . /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
PROMPT_COMMAND='__git_ps1 "\h:\W \u" "\\\$ "'

I had same problem when upgrading to Yosemite.
I just had to modify ~/.bashrc to source /usr/local/etc/bash_completion.d/git-prompt.sh instead of the old path.
then re-source your . ~/.bashrc to get the effect.

__git_ps1 for bash is now found in git-prompt.sh in /usr/local/etc/bash_completion.d on my brew installed git version 1.8.1.5

this works in OS 10.8 in the .bash_profile
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='YOURNAME[\W]$(__git_ps1 "(%s)"): '
fi

For macports I had to add: source /opt/local/share/git-core/git-prompt.sh to my ./profile

If you're hoping to use Homebrew to upgrade Git and you've let your system become out-of-date in general (as I did), you may need to bring Homebrew itself up-to-date first (as per brew update: The following untracked working tree files would be overwritten by merge: thanks #chris-frisina)
First bring Homebrew into line with the current version
cd /usr/local
git fetch origin
git reset --hard origin/master
Then update Git:
brew upgrade git
Problem Solved! ;-)

At least with Xcode 6, you already have git-completion.bash. It's inside the Xcode app bundle.
Just add this to your .bashrc:
source `xcode-select -p`/usr/share/git-core/git-completion.bash

Download the files git-prompt.sh and git-completion.bash from this Git completion
Rename the files.
Move those files to your home directory.
Add the source file in to the .bash_profile
source ~/git-completion0.bash
source ~/git-prompt0.sh
and four to trigger the code block.

I know it's not a real answer...
I had some strange issues with sourcing git-prompt.sh in my .bashrc so I started to look for other solution. This one: http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ doesn't use __git_ps1 and author claims it works also on Mac (for now it works perfectly on my Ubuntu and it's easy to tweak).
I hope it helps!

I was doing the course on Udacity for git hub and was having this same issue. Here is my final code that make is work correctly.
# Change command prompt
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \ . (.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
It works!
https://i.stack.imgur.com/d0lvb.jpg

curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.git-prompt.bash
[[ -f ~/.git-prompt.bash ]] && . ~/.git-prompt.bash
# Available GIT_PS1 options/env vars
cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq | sed -r 's,^(.*)$,export \1=,'
export GIT_PS1_COMPRESSSPARSESTATE=
export GIT_PS1_DESCRIBE_STYLE=
export GIT_PS1_HIDE_IF_PWD_IGNORED=
export GIT_PS1_OMITSPARSESTATE=
export GIT_PS1_SHOWCOLORHINTS=
export GIT_PS1_SHOWDIRTYSTATE=
export GIT_PS1_SHOWSTASHSTATE=
export GIT_PS1_SHOWUNTRACKEDFILES=
export GIT_PS1_SHOWUPSTREAM=
export GIT_PS1_STATESEPARATOR=
for i in $(cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq); do varname=$i; declare -g ${i}=1; done
# P.S Above is just illustration not all config vars are [0/1].
# For more info:
cat ~/.git-prompt.bash | sed -r -n -e '1,/^[^\s*#]/p' | head -n -2

This one worked for me, and it has colored git output and an indicator in the prompt whether files have changed / been added, right baked into it:
GIT_PS1_SHOWDIRTYSTATE=true
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-completion.bash
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-prompt.sh
PS1='\[\033[32m\]\u#\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
Be sure to use the correct path! I used homebrew to install git, use brew list git to get the path to your current installation.
Would be nice not to use a hard coded path, but don't know how to get the path to the current installation.
More infos here: http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/

For git, there are /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh. And please look /etc/bashrc_Apple_Terminal too.
So, I put these in ~/.bash_profile:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }__git_ps1 '\u:\w' '\\\$ '"
fi

Yet another option I just installed on Mojave: magicmonty/bash-git-prompt
Run (brew update) and brew install bash-git-prompt or brew install --HEAD bash-git-prompt
Then to your ~/.bash_profile or ~/.bashrc:
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
GIT_PROMPT_ONLY_IN_REPO=1
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
I'm happy.

Please not that, if you haven't installed git through Xcode or home-brew, you'll likely find the bash scripts haysclarks refers to in /Library/Developer/CommandLineTools/, and not in /Applications/Xcode.app/Contents/Developer/, thus making the lines to include within .bashrc the following:
if [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then
. /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
fi
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
You'll need those lines if you wish to use git-prompt as well.
[1]: https://stackoverflow.com/a/20211241/4795986

Copy/Download the following files and copy them to home directory: ~/
git-completion.bash
git-prompt.sh
For the bash_profile, add this at the beginning:
source ~/git-completion.bash
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
For more and easy downloads you could check this.

Related

Git for Windows doesn't execute my .bashrc file

I've just installed Git for Windows 2.5.0 on Windows 7, and it appears that my .bashrc file is not being executed when I run Git Bash.
I created the file like so:
Administrator#HintTech-Dev MINGW64 /
$ pwd
/
Administrator#HintTech-Dev MINGW64 /
$ cd ~
Administrator#HintTech-Dev MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator#HintTech-Dev MINGW64 ~
$ touch .bashrc
Administrator#HintTech-Dev MINGW64 ~
$ vi .bashrc
[... I insert the line "ZZZTESTVAR=234" (without the quotes) into the file in vim ...]
Administrator#HintTech-Dev MINGW64 ~
$ exit
Yet, when I next run Git Bash:
Administrator#HintTech-Dev MINGW64 /
$ set | grep ZZZ
Administrator#HintTech-Dev MINGW64 /
$ cat ~/.bashrc
ZZZTESTVAR=234
Administrator#HintTech-Dev MINGW64 /
$ ZZZTESTVAR=234
Administrator#HintTech-Dev MINGW64 /
$ set | grep ZZZ
ZZZTESTVAR=234
Administrator#HintTech-Dev MINGW64 /
$
Why isn't my .bashrc being run? It seems to be in the right place and have the right permissions.
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
Same thing happened to me when I upgraded to Git Bash 2.5.0 in Windows 10. I renamed my '.bashrc' -> '.bash_profile' and relaunched Git Bash. Everything's working as usual again.
mv ~/.bashrc ~/.bash_profile
It appears the latest version of git for Windows (2.8.3.windows.1) uses a 'profile' file now instead of the .bash_profile. I assume this is so it isn't hidden and a valid file name. Didn't seem to cause any issues before, but maybe it was confusing to people.
A bit late on this answer perhaps, but you could call bash with the -rcfile argument followed by the location of your .bashrc file.
bash -rcfile C:\Users\name\.bashrc
I've added this to the end of my PowerShell profile (.ps1 file) so that Powershell automatically opens in bash and configured to my preferences.

Something in my bashrc file causing bash: command not found?

When I open up a new shell I get:
Last login: Sun Mar 23 10:14:46 on ttys000
-bash:  : command not found
I'm not totally sure how to figure out what's going on there, as its not totally clear which command its talking about.
Is it likely something in the .bashrc file?
HISTSIZE=10000
HISTFILESIZE=20000
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
export PS1="\[\e[01;32m\]\h \[\e[01;34m\]\W \$(parse_git_branch)\[\e[01;34m\]$\[\e[00m\] "
export PYTHONSTARTUP=/Users/JimShook/.pythonstartup
export WORKON_HOME=$HOME/.virtualenv
source /usr/local/bin/virtualenvwrapper.sh
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
PATH=$PATH:/usr/local/rvm/bin # Add RVM to PATH for scripting
### Added by the Heroku Toolbelt
Or maybe the bash_profile?
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Any thoughts are helpful.
Things to check - as suggested above:
anything changed recently? if so, start there... (added new line/command/sourced file?)
look for 'control codes' in the your file(s)
Try:
cat -v ~/.bashrc ## look for '^M' or other special chars - remove if present
bash -n ~/.bashrc ## try the 'sourced files' separately
If using VI, some thing to try after opening the file:
:syntax off ## turn off colors - may be easier to see 'hidden' codes
:set list ## control codes may be visible

How can I display the current branch and folder path in terminal?

I've been watching some of the Team Treehouse videos and they have a very nice looking terminal when working with Git.
For example they have (something similar):
mike#treehouseMac: [/Work/test - feature-branch-name] $ git add .
mike#treehouseMac: [/Work/test - feature-branch-name] $ git commit -m "Some feature."
mike#treehouseMac: [/Work/test - feature-branch-name] $ git checkout master
mike#treehouseMac: [/Work/test - master] $ git status
How can my terminal show me some useful information of what branch I'm on, with colors to distinguish bits of the data I want? Is there some sort of de-facto plugin I haven't found yet?
I'm using Mac OSX 10.8
For anyone looking for how to do this in macOS Catalina or above (10.15+ incl. Big Sur 11.0) which has deprecated bash in favour of zsh, here is my .zshrc file:
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n#%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
If you don't like the colours I have used, replace the 243/197/39 values with the colour codes as defined here:
https://misc.flogisoft.com/bash/tip_colors_and_formatting
Simple way
Open ~/.bash_profile in your favorite editor and add the following content to the bottom.
Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u#\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
Add Git Branch To Terminal Prompt (Mac)
It's not about a plugin. It's about prompt tricks in the shell.
For a cool setup in bash, check out the dotfiles project of this guy:
https://github.com/mathiasbynens/dotfiles
To get a fancy prompt, include the .bash_prompt in your ~/.bash_profile or ~/.bashrc.
To get the exact same prompt as in your question, change the export PS1 line at the end of .bash_prompt like this:
export PS1="\[${BOLD}${MAGENTA}\]\u\[$WHITE\]#\[$ORANGE\]\h\[$WHITE\]: [\[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" - \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]] \$ \[$RESET\]"
I ended up using all the .bash* files from this repository about a month ago, and it's been really useful for me.
For Git, there are extra goodies in .gitconfig.
And since you're a mac user, there are even more goodies in .osx.
To expand on the existing great answers, a very simple way to get a great looking terminal is to use the open source Dotfiles project.
https://github.com/mathiasbynens/dotfiles
Installation is dead simple on OSX and Linux. Run the following command in Terminal.
git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
This is going to:
Git clone the repo.
cd into the folder.
Run the installation bash script.
For Mac Catilina 10.15.5 and later version:
add in your ~/.zshrc file
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '
My prompt includes:
Exit status of last command (if not 0)
Distinctive changes when root
rsync-style user#host:pathname for copy-paste goodness
Git branch, index, modified, untracked and upstream information
Pretty colours
Example:
To do this, add the following to your ~/.bashrc:
#
# Set the prompt #
#
# Select git info displayed, see /usr/share/git/completion/git-prompt.sh for more
export GIT_PS1_SHOWDIRTYSTATE=1 # '*'=unstaged, '+'=staged
export GIT_PS1_SHOWSTASHSTATE=1 # '$'=stashed
export GIT_PS1_SHOWUNTRACKEDFILES=1 # '%'=untracked
export GIT_PS1_SHOWUPSTREAM="verbose" # 'u='=no difference, 'u+1'=ahead by 1 commit
export GIT_PS1_STATESEPARATOR='' # No space between branch and index status
export GIT_PS1_DESCRIBE_STYLE="describe" # detached HEAD style:
# contains relative to newer annotated tag (v1.6.3.2~35)
# branch relative to newer tag or branch (master~4)
# describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
# default exactly eatching tag
# Check if we support colours
__colour_enabled() {
local -i colors=$(tput colors 2>/dev/null)
[[ $? -eq 0 ]] && [[ $colors -gt 2 ]]
}
unset __colourise_prompt && __colour_enabled && __colourise_prompt=1
__set_bash_prompt()
{
local exit="$?" # Save the exit status of the last command
# PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1
local PreGitPS1="${debian_chroot:+($debian_chroot)}"
local PostGitPS1=""
if [[ $__colourise_prompt ]]; then
export GIT_PS1_SHOWCOLORHINTS=1
# Wrap the colour codes between \[ and \], so that
# bash counts the correct number of characters for line wrapping:
local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]'
local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]'
local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]'
local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]'
local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]'
local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]'
local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]'
local None='\[\e[0m\]' # Return to default colour
# No username and bright colour if root
if [[ ${EUID} == 0 ]]; then
PreGitPS1+="$BRed\h "
else
PreGitPS1+="$Red\u#\h$None:"
fi
PreGitPS1+="$Blu\w$None"
else # No colour
# Sets prompt like: ravi#boxy:~/prj/sample_app
unset GIT_PS1_SHOWCOLORHINTS
PreGitPS1="${debian_chroot:+($debian_chroot)}\u#\h:\w"
fi
# Now build the part after git's status
# Highlight non-standard exit codes
if [[ $exit != 0 ]]; then
PostGitPS1="$Red[$exit]"
fi
# Change colour of prompt if root
if [[ ${EUID} == 0 ]]; then
PostGitPS1+="$BRed"'\$ '"$None"
else
PostGitPS1+="$Mag"'\$ '"$None"
fi
# Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1
__git_ps1 "$PreGitPS1" "$PostGitPS1" '(%s)'
# echo '$PS1='"$PS1" # debug
# defaut Linux Mint 17.2 user prompt:
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 "(%s)") \$ '
}
# This tells bash to reinterpret PS1 after every command, which we
# need because __git_ps1 will return different text and colors
PROMPT_COMMAND=__set_bash_prompt
In 2019, I think git branch --show-current is a better command than the accepted answer.
$ git branch --show-current
master
(Added in git 2.22 release in June 2019)
It runs much faster as it doesn't need to iterate through all branches. Similarly git branch should be avoided too in the command prompt as it slows down your prompt if you have many local branches.
Put it in a function to use anywhere on command prompt:
# This function returns '' in all below cases:
# - git not installed or command not found
# - not in a git repo
# - in a git repo but not on a branch (HEAD detached)
get_git_current_branch() {
git branch --show-current 2> /dev/null
}
More context:
$ git version
git version 2.23.0
Just Install the oh-my-zsh plugins as described in this link.
It works best on macOS and Linux.
Basic Installation
Oh My Zsh is installed by running one of the following commands in your terminal. You can install this via the command-line with either curl or wget.
via curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
via wget
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
for anyone still looking for this , i just installed ohmyz
https://ohmyz.sh/#install and the branches it's showing
In the new Catalina OS for Mac
i) zsh way. Add below lines in .zshrc
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
COLOR_DEF='%f'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{33}'
setopt PROMPT_SUBST
export PROMPT='${COLOR_DIR}%1d${COLOR_DEF}${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
ii) Or to use old bash, you need to change
System Preference -> Users & Groups -> Right click user user
-> Advanced Option -> Login shell -> /bin/bash
Write .bash_profile as below and restart the system
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
export PS1="\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Output: FolderName BranchName $
The git package installed on your system includes bash files to aid you in creating an informative prompt. To create colors, you will need to insert terminal escape sequences into your prompt. And, the final ingredient is to update your prompt after each command gets executed by using the built-in variable PROMPT_COMMAND.
Edit your ~/.bashrc to include the following, and you should get the prompt in your question, modulo some color differences.
#
# Git provides a bash file to create an informative prompt. This is its standard
# location on Linux. On Mac, you should be able to find it under your Git
# installation. If you are unable to find the file, I have a copy of it on my GitHub.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-git-prompt.sh
#
source /usr/share/git/completion/git-prompt.sh
#
# Next, we need to define some terminal escape sequences for colors. For a fuller
# list of colors, and an example how to use them, see my bash color file on my GitHub
# and my coniguration for colored man pages.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/10-colors.sh
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-less.sh
#
color_start='\e['
color_end='m'
color_reset='\e[0m'
color_bg_blue='44'
#
# To get a fancy git prompt, it's not sufficient to set PS1. Instead, we set PROMPT_COMMAND,
# a built in Bash variable that gets evaluated before each render of the prompt.
#
export PROMPT_COMMAND="PS1=\"\${color_start}\${color_bg_blue}\${color_end}\u#\h [\w\$(__git_ps1 \" - %s\")]\${color_reset}\n\$ \""
#
# If you find that the working directory that appears in the prompt is ofter too long,
# then trim it.
#
export PROMPT_DIRTRIM=3
There are many PS1 generators but ezprompt has the git status (2nd tab 'Status Elements' ) also.
For macOS:
Step 1:
Create and edit a .zshrc file that will be used for terminal configuration.
touch ~/.zshrc; open ~/.zshrc
Step 2:
Add this to your ~/.zshrc file:
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '
Based on 6LYTH3's answer I've decided to post my own due to some improvements that may come in handy:
Simple solution
Open ~/.bash_profile and add the following content
# \[\e[0m\] resets the color to default color
reset_color='\[\e[0m\]'
# \[\033[33m\] sets the color to yellow
path_color='\[\033[33m\]'
# \e[0;32m\ sets the color to green
git_clean_color='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
git_dirty_color='\[\e[0;31m\]'
# determines if the git branch you are on is clean or dirty
git_prompt ()
{
# Is this a git directory?
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
# Grab working branch name
git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
git_color="${git_clean_color}"
else
git_color="${git_dirty_color}"
fi
echo " [$git_color$git_branch${reset_color}]"
}
export PS1="${path_color}\w\[\e[0m\]$(git_prompt)\n"
This should:
1) Prompt the path you're in, in color: path_color.
2) Tell you which branch are you.
3) Color the name of the branch based on the status of the branch with git_clean_color
for a clean work directory and git_dirty_color for a dirty one.
4) The brackets should stay in the default color you established in your computer.
5) Puts the prompt in the next line for readability.
You can customize the colors with this list
Sophisticated Solution
Another option is to use Git Bash Prompt, install with this. I used the option via Homebrew on Mac OS X.
git_prompt_list_themes to see the themes but I didn't like any of them.
git_prompt_color_samples to see available colors.
git_prompt_make_custom_theme [<Name of base theme>] to create a new custom theme, this should create a .git-prompt-colors.sh file.
subl ~/.git-prompt-colors.sh to open git-prompt-colors.sh and customize:
The .git-prompt-colors.sh file should look like this with my customization
override_git_prompt_colors() {
GIT_PROMPT_THEME_NAME="Custom"
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
GIT_PROMPT_BRANCH="${Green}"
else
GIT_PROMPT_BRANCH="${Red}"
fi
}
reload_git_prompt_colors "Custom"
Hope this helps, have a great day!
In Catalina and above, you can open your .zshrc file by running nano ~/.zshrc, and posting the following at the top of the document:
# Show git branch in terminal
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;220m’
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~
${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
Exit and save. Restart your terminal, or run exec zsh and that should be it.
OR
You can check out this theme and see if you like it. It is really easy to install and looks good in my opinion:
https://github.com/romkatv/powerlevel10k#configuration
Hope that helps.
From Mac OS Catalina
.bash_profile is replaced with .zprofile
Step 1:
Create a .zprofile
touch .zprofile
Step 2:
nano .zprofile
type below line in this
source ~/.bash_profile
and save(ctrl+o return ctrl+x)
Step 3:
Restart your terminal
To Add Git Branch Name
Now you can add below lines in .bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u#\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
Restart your terminal this will work.
Note:
Even you can rename .bash_profile to .zprofile that also works.
Keep it fast, keep it simple
put this in your ~/.bashrc file.
git_stuff() {
git_branch=$(git branch --show-current 2> /dev/null)
if [[ $git_branch == "" ]];then
echo -e ""
elif [[ $git_branch == *"Nocommit"* ]];then
echo -e "No commits"
else
echo -e "$git_branch"
fi
}
prompt() {
PS1="\e[2m$(date +%H:%M:%S.%3N) \e[4m$(git_stuff)\033[0m\n\w$ "
}
PROMPT_COMMAND=prompt
Then source ~/.bashrc
Did some searching & then adjusted it a bit & settled with this.
vi ~/.zshrc
And within zshrc file ->
function git_branch_name()
{
branch=$(git symbolic-ref HEAD 2> /dev/null | awk 'BEGIN{FS="/"} {print $NF}')
if [[ $branch == "" ]];
then
:
else
echo '- ('$branch')'
fi
}
setopt prompt_subst
prompt='%n %1/ $(git_branch_name) $ '

(Mac) -bash: __git_ps1: command not found

I'm trying to change my command promt in terminal. I keep getting the error:
-bash: __git_ps1: command not found
I've tried it just by typing it into the terminal as is: __git_ps1. I've also tried it out in the .bash_profile
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
As you might be able to see/tell, yes, I do have the auto-completion installed and it does work great!
I came across this question: " PS1 env variable does not work on mac " which gives the code
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
So I add it to my .bash_profile hoping that it will change something. Well, it did. It just changed the error output.
Here's the .bash_profile with the addition:
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
And now here's the changed error output:
sed: (%s): No such file or directory
Note: I've also moved the alias below the source with no difference. I have git version 1.7.12.1
This should be a simple change. Can someone please help me?
Edit 10/13/12
No, I definitely do not want to define __git_ps1 myself but was just trying to see if it would be recognized by doing so. Yes, I have the .git-completion.bash file installed. Here's how I got auto completion on my machine.
cd ~
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git.completion.bash ~/.git-completion.bash
A ls -la then lists the .git-completion.bash file.
Edit 10/13/12 - Solved by Mark Longair (below)
The following code worked for me in the .bash_profile while others did not...
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='Geoff[\W]$(__git_ps1 "(%s)"): '
fi
You've installed the version of git-completion.bash from master - in git's development history this is after a commit that split out the __git_ps1 function from the completion functionality into a new file (git-prompt.sh). The commit that introduced this change, which explains the rationale, is af31a456.
I would still suggest that you just source the version of git-completion.bash (or git-prompt.sh) that is bundled with your installation of git.
However, if for some reason you still want to use this functionality by using scripts separately downloaded from master, you should download git-prompt.sh similarly:
curl -o ~/.git-prompt.sh \
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
... and add the following line to your ~/.bash_profile:
source ~/.git-prompt.sh
Then your PS1 variable that includes __git_ps1 '%s' should work fine.
After upgrading to OSX 10.9 Mavericks I had to reference the following files to get git shell command completion and git prompt to work again.
From my .bash_profile or similar:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
fi
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
#shell prompt example
PS1='\u $(__git_ps1 "(%s)")\$ '
You should
$ brew install bash bash-completion git
Then source "$(brew --prefix)/etc/bash_completion" in your .bashrc.
Following worked for me like a charm:
Run following in your Terminal:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
Open/Create bash_profile:
$ vi ~/.bash_profile
Add following to the file:
source ~/.bash_git
export PS1='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[00m\] '
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
Finally, source it using:
$ source ~/.bash_profile
This will solve the problem of bash: __git_ps1: command not found.
Also your prompt will change to "os ". To change "os" to something else, modify "os" string in export PS1 line.
Solution for MacOS Sierra and git version 2.10.1 <2017-2-06>
Step 1: Install the Git
You can skip this step if you already installed the latest git.
Download git package from browser https://git-scm.com/download/
Note: if you install with curl [option] https://... option to download, you would have to make sure your system support SSL. So for new comer, to download from browser and install directly from git installer is much easier.
Installation Verification:
Show where is your git directory at: which git
Show which version your git currently is: git --version current version should be 2.10.1.
Step 2: Add your git profile to your shell
Open your shell profile:
nano ~/.bash_profile or nano ~/.bashrc Depends on where your modification is.
Add the following code to the file:
source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
Note: git installation location changed from opt/ directory to usr/local/ after OSX upgrade to El Capitain, and this is why some of the old answer above doesn't work anymore in MacOS Sierra.
Add the following code to your PS1 configuration:
Option 1: add directly to your PS1: export PS1='\w$(__git_ps1 "(%s)") > '
I prefer this simple approach since I already know the .git-completion.bash is there in my home directory, and I can add other prompt format in the front of it. here is my personal prompt for your reference: export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
Option 2: Add a selection script
if [ -f ~/.git-completion.bash ]; then
export PS1='\w$(__git_ps1 "(%s)") > '
fi
Save and use the profile: source ~/.bash_profile or source ~/.bashrc
Now you should see the git prompt working properly and shows which branch you are in right now.
High Sierra clean solution with colors !
No downloads. No brew. No Xcode
Just add it to your ~/.bashrc or ~/.bash_profile
export CLICOLOR=1
[ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh ] && . /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
PROMPT_COMMAND='__git_ps1 "\h:\W \u" "\\\$ "'
I had same problem when upgrading to Yosemite.
I just had to modify ~/.bashrc to source /usr/local/etc/bash_completion.d/git-prompt.sh instead of the old path.
then re-source your . ~/.bashrc to get the effect.
__git_ps1 for bash is now found in git-prompt.sh in /usr/local/etc/bash_completion.d on my brew installed git version 1.8.1.5
this works in OS 10.8 in the .bash_profile
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='YOURNAME[\W]$(__git_ps1 "(%s)"): '
fi
For macports I had to add: source /opt/local/share/git-core/git-prompt.sh to my ./profile
If you're hoping to use Homebrew to upgrade Git and you've let your system become out-of-date in general (as I did), you may need to bring Homebrew itself up-to-date first (as per brew update: The following untracked working tree files would be overwritten by merge: thanks #chris-frisina)
First bring Homebrew into line with the current version
cd /usr/local
git fetch origin
git reset --hard origin/master
Then update Git:
brew upgrade git
Problem Solved! ;-)
At least with Xcode 6, you already have git-completion.bash. It's inside the Xcode app bundle.
Just add this to your .bashrc:
source `xcode-select -p`/usr/share/git-core/git-completion.bash
Download the files git-prompt.sh and git-completion.bash from this Git completion
Rename the files.
Move those files to your home directory.
Add the source file in to the .bash_profile
source ~/git-completion0.bash
source ~/git-prompt0.sh
and four to trigger the code block.
I know it's not a real answer...
I had some strange issues with sourcing git-prompt.sh in my .bashrc so I started to look for other solution. This one: http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ doesn't use __git_ps1 and author claims it works also on Mac (for now it works perfectly on my Ubuntu and it's easy to tweak).
I hope it helps!
I was doing the course on Udacity for git hub and was having this same issue. Here is my final code that make is work correctly.
# Change command prompt
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \ . (.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
It works!
https://i.stack.imgur.com/d0lvb.jpg
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.git-prompt.bash
[[ -f ~/.git-prompt.bash ]] && . ~/.git-prompt.bash
# Available GIT_PS1 options/env vars
cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq | sed -r 's,^(.*)$,export \1=,'
export GIT_PS1_COMPRESSSPARSESTATE=
export GIT_PS1_DESCRIBE_STYLE=
export GIT_PS1_HIDE_IF_PWD_IGNORED=
export GIT_PS1_OMITSPARSESTATE=
export GIT_PS1_SHOWCOLORHINTS=
export GIT_PS1_SHOWDIRTYSTATE=
export GIT_PS1_SHOWSTASHSTATE=
export GIT_PS1_SHOWUNTRACKEDFILES=
export GIT_PS1_SHOWUPSTREAM=
export GIT_PS1_STATESEPARATOR=
for i in $(cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq); do varname=$i; declare -g ${i}=1; done
# P.S Above is just illustration not all config vars are [0/1].
# For more info:
cat ~/.git-prompt.bash | sed -r -n -e '1,/^[^\s*#]/p' | head -n -2
This one worked for me, and it has colored git output and an indicator in the prompt whether files have changed / been added, right baked into it:
GIT_PS1_SHOWDIRTYSTATE=true
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-completion.bash
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-prompt.sh
PS1='\[\033[32m\]\u#\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
Be sure to use the correct path! I used homebrew to install git, use brew list git to get the path to your current installation.
Would be nice not to use a hard coded path, but don't know how to get the path to the current installation.
More infos here: http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/
For git, there are /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh. And please look /etc/bashrc_Apple_Terminal too.
So, I put these in ~/.bash_profile:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }__git_ps1 '\u:\w' '\\\$ '"
fi
Yet another option I just installed on Mojave: magicmonty/bash-git-prompt
Run (brew update) and brew install bash-git-prompt or brew install --HEAD bash-git-prompt
Then to your ~/.bash_profile or ~/.bashrc:
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
GIT_PROMPT_ONLY_IN_REPO=1
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
I'm happy.
Please not that, if you haven't installed git through Xcode or home-brew, you'll likely find the bash scripts haysclarks refers to in /Library/Developer/CommandLineTools/, and not in /Applications/Xcode.app/Contents/Developer/, thus making the lines to include within .bashrc the following:
if [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then
. /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
fi
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
You'll need those lines if you wish to use git-prompt as well.
[1]: https://stackoverflow.com/a/20211241/4795986
Copy/Download the following files and copy them to home directory: ~/
git-completion.bash
git-prompt.sh
For the bash_profile, add this at the beginning:
source ~/git-completion.bash
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
For more and easy downloads you could check this.

bash completion of makefile target

Suppose I have a simple makefile like:
hello:
echo "hello world"
bye:
echo "bye bye"
Then in bash I want something like:
make h < tab >
so it can complete to
make hello
I found a simple way like creating empty files hello and bye but I'm looking for something more sophisticated.
Add this in your ~/.bash_profile file or ~/.bashrc file
complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sed 's/[^a-zA-Z0-9_.-]*$//'\`" make
This searches for a target in your Makefile titled 'Makefile' or 'makefile' (note the capital ? wildcard in ?akefile) using grep, and pipes it over to the complete command in bash which is used to specify how arguments are autocompleted. The -W flag denotes that the input to the complete command will be a wordlist which is accomplished by passing the results of grep through sed which arranges it into the desirable wordlist format.
Caveats and gotchas:
Your make file is named 'GNUMakefile' or anything else other than 'Makefile' or 'makefile'. If you frequently encounter such titles consider changing the regular expression ?akefile accordingly.
Forgetting to source your ~/.bash_profile or ~/.bashrc file after making the changes. I add this seemingly trivial detail since, to the uninitiated it is unfamiliar.
For any change to your bash files to take effect, source them using the command
source ~/.bashrc
or
source ~/.bash_profile
PS. You also now have the added ability to display the possible make targets by pressing [Tab] twice just like in bash completion. Just make sure you add a space after the command make before typing [Tab] twice.
This answer from 2010 is outdated - the project mentioned here seems to have been discontinued.
Could this be what you're looking for?
http://freshmeat.net/projects/bashcompletion/
make [Tab] would complete on all
targets in Makefile. This project was
conceived to produce programmable
completion routines for the most
common Linux/UNIX commands, reducing
the amount of typing sysadmins and
programmers need to do on a daily
basis.
There's a useful package called bash-completion available for most every OS. It includes Makefile completion.
(If you're using macOS and Homebrew, you can get this via brew install bash-completion.)
This seems to be default in at least Debian Lenny:
$ grep Makefile /etc/bash_completion
# make reads `GNUmakefile', then `makefile', then `Makefile'
elif [ -f ${makef_dir}/Makefile ]; then
makef=${makef_dir}/Makefile
# before we scan for targets, see if a Makefile name was
# deal with included Makefiles
The header of this file states:
# The latest version of this software can be obtained here:
#
# http://bash-completion.alioth.debian.org/
#
# RELEASE: 20080617.5
Here is a completion script that looks at the .PHONY: declaration.
_make_phony_words() {
local opt_revert
if [ -n "${BASH_VERSION:-}" ]; then
shopt -q nullglob || {
opt_revert=1 ; shopt -s nullglob ;
}
elif [ -n "${ZSH_VERSION:-}" ]; then
[[ -o nullglob ]] || {
opt_revert=1 ; setopt nullglob
}
fi
for f in ./?akefile ./*.make ; do
sed -nEe '/^.PHONY/ { s/^.PHONY:[ ]?// ; p ; } ' "$f" | tr ' ' $'\n' | sort -u
done
if [ -n "$opt_revert" ]; then
[ -n "${ZSH_VERSION:-}" ] && unsetopt nullglob
[ -n "${BASH_VERSION:-}" ] && shopt -u nullglob
fi
unset opt_revert
}
_make_phony_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY+=( $(compgen -W "$( _make_phony_words )" -- ${cur}) )
}
complete -F _make_phony_complete make
Makefile completion on steroids!
I had 2 problems with the normal completions:
Problem #1
Sometimes you have targets you want to call like make greet:hi and make greet:hola sort of like namespacing Makefile target names. So your Makefile ends up looking like:
greet\:hola:
echo "hola world"
# OR a .PHONY target
.PHONY: greet\:hi
greet\:hi:
echo "hi world"
In this case the auto-completions after : don't show up as it uses \: in the Makefile as shown above.
Problem #2
There wasn't a way to navigate through the list of all Makefile targets that match my input using arrow keys (or CTRL-p / CTRL-n) in my bash shell.
Basically, I wanted to use fuzzy search like approach on the targets (i.e. fzf).
FZF Repo: https://github.com/junegunn/fzf
Solution
Install FZF Dependency
Using Homebrew
You can use Homebrew (on macOS or Linux)
to install fzf.
brew install fzf
$(brew --prefix)/opt/fzf/install
Using Linux package managers
Package Manager
Linux Distribution
Command
APK
Alpine Linux
sudo apk add fzf
APT
Debian 9+/Ubuntu 19.10+
sudo apt-get install fzf
Conda
conda install -c conda-forge fzf
DNF
Fedora
sudo dnf install fzf
Nix
NixOS, etc.
nix-env -iA nixpkgs.fzf
Pacman
Arch Linux
sudo pacman -S fzf
pkg
FreeBSD
pkg install fzf
pkgin
NetBSD
pkgin install fzf
pkg_add
OpenBSD
pkg_add fzf
XBPS
Void Linux
sudo xbps-install -S fzf
Zypper
openSUSE
sudo zypper install fzf
FZF and : compatible auto-complete command
Put this in your .bashrc
complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+[\\:]*[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sort | uniq | sed 's/[^a-zA-Z0-9_.-]*$//' | sed 's/[\]//g' | fzf\`" make
Now just typing make and then hitting the key will work!
DEMO: in action!
Then you can use as following:
make using fzf
I added so I follow "include" directives in Makefile. So my .bashrc looks like this:
function followMakefile() {
grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sed 's/[^a-zA-Z0-9_.-]*$//'
for x in `grep -E '^include' ?akefile | sed 's/include //'`
do
grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' $x | sed 's/[^a-zA-Z0-9_.-]*$//'
done
}
complete -W "\`followMakefile\`" make
In Ubuntu 10.04, source the following file:
. /etc/bash_completion
or uncomment it in
/etc/bash.bashrc

Categories

Resources