Skip to main content

Add commit hash to command line prompt in zsh, one line in .zshrc

Updated by Tim Rabbetts on
zsh prompt

Customize Your ZSH Prompt to Display the Last Commit's SHA

I enjoy customizing my terminal, and one feature I find particularly useful is displaying the last commit's SHA directly in the command line. Here's how I achieved this in ZSH.

Setting Up the Environment

I recently set up a new development environment on a fresh machine, starting with terminator and the Oh My Zsh framework, using the Agnoster theme. After configuring the terminal to my liking, I noticed one missing feature: displaying the last commit's short SHA after the branch name.

Adding the Commit SHA to Your Prompt

To display the last commit's short SHA, you can utilize the vcs_info function, which is part of ZSH's internals. This function interacts with version control systems like Git to retrieve and display relevant information in your terminal prompt.

Here’s the key line you need to add to your .zshrc file:

zstyle ':vcs_info:git:*' formats ' %8.8i'

What This Line Does:

  • zstyle: A command that configures various ZSH settings.
  • :vcs_info:git:*: Targets all Git repositories in your system.
  • formats ' %8.8i': Specifies the format of the prompt. The %8.8i part retrieves the first 8 characters of the commit SHA (short SHA).

After adding this line, reload your terminal by running:

source ~/.zshrc

 

Add new comment