How to shorten zsh prompt (oh my zsh)

Shan Dou
4 min readFeb 3, 2019

Problem Description: Full path being used as prompt, and it gets really long

Oh My Zsh has added plenty of delight to my terminal work since I switched from bash last year. The agnoster theme has been a joy to use, but the default settings use the full path of my current directory location as the command line prompt. As you can easily imagine, if I am a few levels deep into a directory, the prompt gets very long and can run across the terminal width. The first time when I tried to solve this problem for my workstation at work, I followed a github thread, got it fixed, but that left behind two issues that I am now writing this article on:

  1. I did the fix in a rush and did not take notes for my future self… Later when I need to do it again for my home workstation, it took me quite a while to re-google and find the fix… 😞 The takeaway? Shan, write it down! Always!!
  2. The fix I am going to talk about becomes an unstaged change that later caused problems when oh-my-zsh itself needs periodic upgrades. This is — in my opinion — a quirk of the the oh-my-zsh installation that might have a cleaner solution that I don’t yet know. If anyone who has run across this article knows a better fix, please leave a comment or message me. I’d appreciate that A LOT 😃

Stepping through fixes

How to shorten the long prompt?

  1. Locate your oh-my-zsh install folder.
    If you already know this with high confidence, great! cd to your folder and move on to the next step. If you are not very sure, go to your ~/.zshrc file and look for the following line:
Figure 1: Find your installation location from .zshrc

2. Great, let’s find the agnoster theme:
For users of other themes in the theme folder, the changes in step 3 should be similar.

Figure 2: Steps to locate the theme-file-in-use

3. Apply changes to the theme file, save, and source ~/.zshrc

Figure 3: Changes needed to shorten prompt path

Once you save the change and reload your setting via source ~/.zshrc, you should be able to see your shorter prompt taking effects ✨ ✨ ✨

Uh-oh, now we have applied unstaged changes and we can’t upgrade… git stash come to rescue

One quirk of oh-my-zsh installation is that it lives as a git repo in our system, and any subsequent upgrades we shall enjoy now become pulling new changes from the “mother” repo. Since we now have applied changes to shorten the prompt, directly pulling new changes would not be allowed unless we stash the changes. Luckily we only need the following command:

$ cd ~/.oh-my-zsh && git stash
$ upgrade_oh_my_zsh
$ git stash pop

This has worked well for me so far, even though it does not look elegant/clean. If anyone knows a better solution, please show off your knowledge 😃

Recap: Critical steps at a glance

  1. Locate the theme file and apply changes to shorten the prompt
# Step 1: check where oh-my-zsh installation is in ~/.zshrc# Path to your oh-my-zsh installation.
export ZSH="/Users/shandou/.oh-my-zsh"
# Step 2: go to the installation directory and locate the themes sub-directory
cd /Users/shandou/.oh-my-zsh/themes/
# Step 3: edit the following line the theme you are using. Save the file and source ~/.zshrc# Dir: current working directory
prompt_dir() {
#prompt_segment blue $CURRENT_FG '%~'
prompt_segment blue $CURRENT_FG '%2~'
}

2. A workaround to you have unstaged changeserror when you need to update oh-my-zsh

# Step 1: go to oh-my-zsh installation folder and stash the change you have made to the theme
$ cd ~/.oh-my-zsh && git stash
# Step 2: use this command to upgrade
$ upgrade_oh_my_zsh
# Step 3: after upgrade, re-apply your changes that are needed to shorten the prompt
$ git stash pop

--

--