Creating Your Own Oh My Zsh Theme
Oh My Zsh is a popular framework for managing your own terminal to do cool stuff with using Zsh configurations. One of its most powerful features is the ability to create custom themes to personalize your terminal experience. This guide will walk you through the steps to create your own Oh My Zsh theme. Let's start with the clone the ohmyzsh github repository.
Clone the Oh My Zsh Repository
To get started, you need to have Oh My Zsh installed. If you haven’t already done so, follow the official installation guide.
Once installed, clone the repository to have access to the necessary files and directories:
# Clone the Oh My Zsh repository (optional if it's not already installed)
git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh
Navigate to the custom themes directory where your theme files will reside:
cd ~/.oh-my-zsh/custom/themes
Choose a Base Theme
Before creating your custom theme, it’s helpful to start with an existing theme as a reference. You can view the available themes in the themes
directory of the Oh My Zsh repository:
ls ~/.oh-my-zsh/themes
Pick a theme that closely resembles the style or functionality you want. For example, if you like the agnoster
theme, copy it to the custom themes folder:
cp ~/.oh-my-zsh/themes/agnoster.zsh-theme ~/.oh-my-zsh/custom/themes/mytheme.zsh-theme
Now, open your copied theme file in a text editor to begin customizing:
nano ~/.oh-my-zsh/custom/themes/mytheme.zsh-theme
Customize and Add Functions to Your Theme
Basic Customization
You can modify the prompts (PROMPT
and RPROMPT
) in your theme file. For example:
PROMPT='%n@%m %1~ %# '
RPROMPT='%F{yellow}%*%f'
%n
: Username%m
: Hostname%1~
: Current directory (truncated)%#
: Prompt character (# for root, $ for user)%*
: Current time
Refer to the Zsh prompt expansion documentation for more options.
Adding a New Function
You can add custom functions to your theme to display specific information in your prompt. For example, to display the current branch of a Git repository:
Define a function in the theme file:
function git_branch() {
git rev-parse --abbrev-ref HEAD 2>/dev/null
}
Use the function in the PROMPT
variable:
PROMPT='%n@%m %1~ $(git_branch) %# '
Save the file and apply the theme.
Apply Your Custom Theme
To use your custom theme, update the ZSH_THEME
variable in your ~/.zshrc
file:
ZSH_THEME="mytheme"
Apply the changes by restarting your terminal or running:
source ~/.zshrc
Tips for Customization
- Experiment with different colors using
%F{color}
and%f
for text formatting. - Use plugins like
git
to enhance your prompt with additional context. - Test changes incrementally to avoid breaking your theme.
Naviend Theme
# vim:ft=zsh ts=2 sw=2 sts=2
function node_prompt_version {
if [ -f .nvmrc ]; then
if which node &> /dev/null; then
echo "%{$fg_bold[green]%} ⬢ node %{$fg[green]%}$(node -v)%{$fg[green]%} %{$reset_color%}"
fi
fi
}
# Must use Powerline font, for \uE0A0 to render.
ZSH_THEME_GIT_PROMPT_PREFIX=" on %{$fg[magenta]%}\uE0A0 "
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}!"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?"
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_RUBY_PROMPT_PREFIX="%{$fg_bold[red]%}‹"
ZSH_THEME_RUBY_PROMPT_SUFFIX="›%{$reset_color%}"
# Function to check if the current month is in the Halloween period (Sep, Oct, Nov)
function is_halloween_season {
local current_month=$(date +%m)
[[ "$current_month" -ge 9 && "$current_month" -le 11 ]] # September, October, November
}
# Function to check if the current month is in the Christmas period (Dec, Jan, Feb)
function is_christmas_season {
local current_month=$(date +%m)
[[ "$current_month" -eq 12 || "$current_month" -eq 1 || "$current_month" -eq 2 ]] # December, January, February
}
# Function to check if the current month is in the Spring period (Mar, Apr, May)
function is_spring_season {
local current_month=$(date +%m)
[[ "$current_month" -ge 3 && "$current_month" -le 5 ]] # March, April, May
}
# Function to check if the current month is in the Summer period (Jun, Jul, Aug)
function is_summer_season {
local current_month=$(date +%m)
[[ "$current_month" -ge 6 && "$current_month" -le 8 ]] # June, July, August
}
HALLOWEEN_PROMPT='
💀🎃 %{%F{214}%}$(date +"%d.%m.%y - %H:%M")%{%f%} 🔮🦇 $(git_prompt_info)$(virtualenv_prompt_info)$(node_prompt_version)
'
CHRISTMAS_PROMPT='
⛄🎄 %{$fg_bold[white]%}$(date +"%d.%m.%y - %H:%M") 🦌🦌🦌 $(git_prompt_info)$(virtualenv_prompt_info)$(node_prompt_version)
'
SPRING_PROMPT='
🔥🏀 %{%F{202}%}$(date +"%d.%m.%y - %H:%M")%{%f%} 🌻🌱 $(git_prompt_info)$(virtualenv_prompt_info)$(node_prompt_version)
'
SUMMER_PROMPT='
🌞🏖️ %{%F{226}%}$(date +"%d.%m.%y - %H:%M")%{%f%} 🏄♂️🍹 $(git_prompt_info)$(virtualenv_prompt_info)$(node_prompt_version)
'
function set_prompt_based_on_season {
if is_halloween_season; then
PROMPT=$HALLOWEEN_PROMPT
elif is_christmas_season; then
PROMPT=$SUMMER_PROMPT
elif is_spring_season; then
PROMPT=$CHRISTMAS_PROMPT
elif is_summer_season; then
PROMPT=$SPRING_PROMPT
else
# Default prompt for other months (you can set it to any other default, or leave empty)
PROMPT='%{%F{7}%}$(date +"%d.%m.%y - %H:%M") %{%f%}$(git_prompt_info)$(virtualenv_prompt_info)$(node_prompt_version)'
fi
}
set_prompt_based_on_season
RPROMPT='$(ruby_prompt_info)'
My Configurations
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="naviend"
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh
# list all elements function
function cd() {
builtin cd "$1" && ls
}
# idunno
function idunno(){
print "¯¯\_(ツ)_/¯¯"
}
# updating zsh pull from master
function update() {
if [ -d ".oh-my-zsh" ]; then
cd .oh-my-zsh || { echo "Failed to change directory to .oh-my-zsh"; return 1; }
echo "Fetching updates for Oh My Zsh..."
git fetch origin master && git pull origin master
if [ $? -eq 0 ]; then
echo "Oh My Zsh updated successfully!"
else
echo "Failed to update Oh My Zsh. Please check your Git configuration."
return 1
fi
cd - > /dev/null || return 1
else
echo ".oh-my-zsh directory not found in the current path."
return 1
fi
}