Yes, Oh My Zsh is awesome! That’s the first thing I installed when I switched from Bash to Zsh and I used it for a few years.
Past that time, I realized that in my daily use, the only features I was taking advantage was:
Autocompletion and history-based autocompletion using the arrow keys.
The fancy multi-line and colorful user prompt showing the working directory, and the switching color after the fail/success of the previous command execution.
The git repository info at the user prompt.
The
z
command, provided by the ZSH-z plugin.
I couldn’t help myself but thinking that the Oh My Zsh framework was much more than I needed. I decided to remove Oh My Zsh and reset Zsh from scratch, so I could configure and install only the features I needed.
This is a four-part post series explaining how to set up those features on a fresh new Zsh installation:
Customize Zsh Pt.1 - Autocompletion
Customize Zsh Pt.2 - User Prompt
Customize Zsh Pt.3 - Git Info 👈
Customize Zsh Pt.4 - ZSH-z Plugin
Git Prompt Integration
Git is shipped by default with a shell prompt customization script called git-prompt.sh
that is compatible with Zsh.
The script provides the command __git_ps1
that can be called with arguments returning information about the repository and its status. Calling the script from inside the PS1 using command-substitution, enables real-time repository status in your Zsh prompt.
How to Set Up
Create a user custom Zsh plugins directory, and a directory for the
git-prompt.sh
plugin:mkdir -p ~/.zsh/plugins/git
Download the script and save it to the plugins directory created:
From Zsh:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.zsh/plugins/git/git-prompt.sh
From the browser:
https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
(Remember to save into
~/.zsh/plugins/git
directory)Load it in Zsh and enable some configurations by adding the following code into the
.zshrc
file:# PLUGINS source ~/.zsh/plugins/git/git-prompt.sh # git prompt options GIT_PS1_SHOWDIRTYSTATE=true GIT_PS1_SHOWSTASHSTATE=true GIT_PS1_SHOWUNTRACKEDFILES=true GIT_PS1_SHOWUPSTREAM="auto" GIT_PS1_STATESEPARATOR=' ' GIT_PS1_HIDE_IF_PWD_IGNORED=true GIT_PS1_COMPRESSSPARSESTATE=true
(To learn more about the options please check the comments inside
git-prompt.sh
file, line 36 to 106.)Attention: The
# PLUGINS
section inside the.zshrc
file must be placed BEFORE the# USER PROMPT
section to have the__git_ps1
command loaded and available to call from inside the PS1.Call the
__git_ps1
command using command-susbsitution inside PS1:Place following snippet just before the second newline character in the PS1 built in the last post:
% %F{magenta}$(__git_ps1 " %s")%f
PS1 before:
PS1='$NL%B%F{cyan}%3~%f%b$NL%B%(?.%F{green}.%F{red})%(!.#.>)%f%b '
PS1 after:
PS1='$NL%B%F{cyan}%3~%f%b% %F{magenta}$(__git_ps1 " %s")%f$NL%B%(?.%F{green}.%F{red})%(!.#.>)%f%b '
Source the
.zshrc
file again, in the Zsh shell type:source ~/.zshrc
And the user prompt is now aware of git repostories!
Your Zsh shell is becoming a powerful tool!
Part 4 explores how to install the ZSH-z plugin.
Next:
Customize Zsh Pt.4 - ZSH-z Plugin
Useful links & references:
- Zsh Documentation (
man -k zsh
to list each Zsh man page section) - Git in Other Environments - Git in Zsh
Footnotes:
- Follow me on Twitter to get more posts like this and other quick tips in your feed.
- If you have any doubts or tips about this article, I’d appreciate knowing and discussing it via email.
- Do you have any other Cli tips? Would you like to publish that in this blog? Please send an email to cli drops.
- As English is not my native language, I apologize for the errors. Corrections are welcome.
- Contact: cli [@] alldrops [.] info.