Skip to content

Instantly share code, notes, and snippets.

@jstavanja
Last active November 16, 2022 13:13
Show Gist options
  • Save jstavanja/ed31f190338abe0d66b1fcae25660c94 to your computer and use it in GitHub Desktop.
Save jstavanja/ed31f190338abe0d66b1fcae25660c94 to your computer and use it in GitHub Desktop.
πŸ’„ Jaka's prompt zsh theme (with abbreviated folder names in the prompt)
# The original theme this theme is extended from
# is the gallifrey.zsh-theme from oh-my-zsh.
# To use this theme, make a new file called jakas-prompt.zsh-theme
# in the directory ~/.oh-my-zsh/themes, and change the THEME variabl
# in your ~/.zshrc to jakas-prompt.
return_code="%(?..%{$fg[red]%}%? ↡%{$reset_color%})"
function dirprompt {
# Function for abbreviating folder names instead of listing
# the pwd output.
#
# Turns /Users/jaka/some/folder into j/s/folder for a shorter
# input in the terminal prompt.
#
# Takes in the current directory,
# gets all of the strings ending with a / character,
# e.g., from /Users/jaka/test it gets Users/ and jaka/.
#
# Then, it extracts the match group 1 from the matches,
# where it matches only the first character in each match,
# e.g., from /Users it gets U and from jaka/ it gets j.
#
# The match gets replaced in the output with the match group 1,
# meaning that each folder name gets substituted with its initial
# letter.
#
# Since the last folder is not matched by the regex, its
# full name remains in the output of the prompt.
#
# To extract only the last few folders, each regex match
# gets a newline character at the end of its substitution
# so that the tail command can then be used.
#
# After using the tail command, we join the lines using tr
# and remove the weird first new line using another tail.
#
# After that, we get rid of the weird output for the home directory,
# and replace // in e.g. //jaka to /, to get e.g. /jaka in the home directory.
pwd | sed -E 's/(.)([^\/]+)\//\1\/\n/g' | tail -3 | tr -d '\n' | tail | tr -s "//" "/"
}
PROMPT='$(dirprompt)~ $(git_prompt_info)%{$reset_color%}%BΒ»%b '
RPS1="${return_code}"
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[yellow]%}β€Ή"
ZSH_THEME_GIT_PROMPT_SUFFIX="β€Ί %{$reset_color%}"
unset return_code host_color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment