Yesterday I came across a webcast for
git. The host of this webinar set out to present git. He started with some background information and later he got to the point of setting up git. He opened his terminal, created a new repository, and cd’ed into the repo’s directory. Then something beautiful happened. The name of his branch showed up in the
prompt statement. It was such an obvious thing to do. Working on multiple branches and moving around, having to check every time if any files need to be committed or stashed can become tedious. But it doesn’t have to be that way. The prompt statement can help us in this process. So, I spent the night looking into it.
my_repo (master*) src/awesome_lib _
Above is what came out of it. Firstly the name of the repository we are in appears. Then, inside the parentheses is the name of the branch we are currently checking out. That name may be followed by an asterisk. The presence of that
* indicates that there are uncommitted/untracked files.
git status -s is used for determining this, so you know what to expect. After that, we have the relative path we are in within the repository. And lastly, it’s the prompt.
username@hostname:~/working/directory $ _
And of course whenever we are outside of a git repository, the prompt “resets” and provides general information again.
This type of customization can extend to whatever other distinguishable directory categories you might have. You do that by adding another elif condition within the if statement of
The secret behind this mod is the
PROMPT_COMMAND. The contents of PROMPT_COMMAND are executed just before
PS1 is displayed. So, we include
set_ps1 within PROMPT_COMMAND in order for set_ps1 to be called the right time and set PS1.
~/.bashrc in your favorite editor. Comment out any lines that try to set/modify PS1. Copy the gist above anywhere within .bashrc. Open a new terminal and you are done.
For more advanced features, take a look at Oh My Zsh.