DEV Community

Cover image for Custom colors in oh-my-zsh themes
Yujin
Yujin

Posted on • Updated on

Custom colors in oh-my-zsh themes

Custom colors in oh-my-zsh themes

So, I was trying to configure my custom zsh theme. I wanted something minimal like displaying the current user, a shortened path and the git info of a directory (if there is).

After a few hours of tinkering, I pretty much got what I wanted but the only thing I didn't like was the colors available.

I wasn't sure which file to look for that contains the fg_bold and fg.

I did try doing a ripgrep: rg fg_bold hoping that I would find a list of available colors. But all I found were .zsh-theme files that were using a fixed set of colors. Magenta, green, blue, yellow, cyan were the ones I found.

Alt Text

I wasn't satisfied with the colors available and I did some trial and error by doing $fg_bold[orange], $fg[pink], etc. But I had no luck.

I did some google search about defining custom colors in zsh and it led me to this GitHub issue: https://github.com/ohmyzsh/ohmyzsh/issues/1101#issuecomment-5450278

It seems that oh-my-zsh uses Spectrum under the hood. The source code can be found under ~/.oh-my-zsh/lib/spectrum.zsh.

Upon checking the source code, I tried running spectrum_ls on my terminal and I was amazed by the output :asto

Alt Text

I can now use custom colors on my custom zsh theme via $FG[<0-255>] e.g. $FG[021]

So, that's it! I just wanted to share how I managed to define custom colors in my zsh setup.

I mainly use Python and Javascript so I did add a few customizations like displaying the node and python version of the current environment.

I made a minimal version of the theme I was working on for those who might be interested: https://github.com/yujinyuz/dotfiles/blob/master/zsh/themes/jpro-minimal.zsh-theme

Alt Text

You can check out the documentation on adding themes to oh-my-zsh: https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#overriding-and-adding-themes

Thanks for reading! It was a great experience writing my first article here on dev.to after being a member for about 2 years now 😅

Top comments (9)

Collapse
 
zoltankundi profile image
Zoltan Kundi

Thanks for the tip! How can I make a custom color bold? I tried it with $FG_BOLD[<#>] but it doesn't seem to work.

Collapse
 
yujinyuz profile image
Yujin

try %{$fg_bold[blue]%}

Collapse
 
fermendezmx profile image
Fernando Méndez

You can do it as follows: %B$FG[<#>]

Collapse
 
fgod1983 profile image
FGOD1983

Nice! is there a way I can use this to costumize the Git coloring?

Collapse
 
yujinyuz profile image
Yujin

Are you talking about the ones in ~/.gitconfig ? I think you could use It

Collapse
 
fgod1983 profile image
FGOD1983 • Edited

no I meant the theme colors used by oh-my-zsh when working on git stuff. but in the meantime I finally found the answers :) you can change that in the theme or in .zshrc with the following values (in my case of using powerlevel9k) and edit the color numbers(which are also using the spectrum color schemes):

POWERLEVEL9K_VCS_CLEAN_BACKGROUND=2
POWERLEVEL9K_VCS_MODIFIED_BACKGROUND=3
POWERLEVEL9K_VCS_UNTRACKED_BACKGROUND=2
POWERLEVEL9K_VCS_CONFLICTED_BACKGROUND=3
POWERLEVEL9K_VCS_LOADING_BACKGROUND=8

Thread Thread
 
yujinyuz profile image
Yujin

That's awesome! Others might find this useful. Thanks for sharing that as well :)

Thread Thread
 
fgod1983 profile image
FGOD1983

no problem :)

Collapse
 
sh4hids profile image
Shahidul Islam Majumder

Thank you very much for the tips.