Monthly Archives: October 2019

Posh-git on Mac using Oh My Zsh Themes

This post explains how to have posh-git prompt style in Oh My Zsh theme on Mac.

After 4 years of using Windows, I am coming back to using a Mac. And there are so many things in Windows I am missing already. One of these things is posh-git; I loved how in one glance to your prompt you know the status of your git repo: how many files changed, how many added, how many deleted, how many indexed… just love it!

Once I moved to Mac, I changed my shell to use zsh using Oh My Zsh due to the rich experience it brings to the terminal. I was delighted to see all these themes and plugins, and then started looking for a theme that provided the same information posh-git prompt provided. To my surprise, there was none! So I started my quest to see how I can change zsh, the theme, or the plugin to have such prompt.

A posh-git prompt that shows the number of files index and changed.

Being lazy, I wanted change an existing theme I like with the least amount of investment. I looked in the documentation to see how I could do that, and found the customisation wiki page:

Should I override the theme?

Overriding the theme seemed to be the perfect solution, however, there were couple of drawbacks:

  • When you override a theme, you override the theme, period! This means that if the author changes something after you have overridden it, you will not get these new changes.
  • It was a little bit too much for me to grasp! When I looked at avit theme as an example, I had questions like what is PROMPT and PROMPT2? What are all these special characters? Where is the reference/documentation to all of these? Are they theme-specific, or are they part of zsh theme reference?

Remember I wanted to put the least amount of effort, and I surely didn’t want to learn the whole thing! But while looking into avit theme, one thing grasped my attention: there was a clear reference to what seemed to be like a function git_prompt_info. And I thought this should be it, if I could find where this function is and how to override it.

To my luck, it was mentioned as an example in the customisation wiki page as an example!

Override the internals it is!

Ok great, now I know that I can customise git_prompt_info, all what I need is to mimic whatever posh-git does in that function!

So I hit google duckduckgo again on the hope that someone already did this, and oh my! I found that there is already a port of it on bash. That’s great, now what should I do? Replace the call of prompt_git_info in the theme with a call to __posh_git_ps1? Or should I call it from prompt_git_info? Since prompt_git_info is an internal lib function, it is probably used in many themes, thus it will make sense to just call __posh_git_pst from within. And to my good surprise, there is a GitHub issue in the posh-git-bash repo that discusses integrating with zsh, it’s even referenced in the main README.md file of the repo.

Initially I mistakenly called the __posh_git_ps1 function, but I soon realised that I need to print (echo) the git info just like prompt_git_info did rather than changing any variables, for that I should use the __posh_git_echo.

And thus I ended up with a file called emad-git-prompt.zsh under the path ~/.oh-my-zsh/custom with the content of posh-git-bash here, and at the end of the file I wrote the following code:

git_prompt_info () {

__posh_git_echo

}

I hope this helps you :)