Skip to content

Instantly share code, notes, and snippets.

@bytrangle
Last active June 26, 2021 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytrangle/40cc24a95436112c5dea0e3c9d2275ad to your computer and use it in GitHub Desktop.
Save bytrangle/40cc24a95436112c5dea0e3c9d2275ad to your computer and use it in GitHub Desktop.
Function to get a program version and display it in the Zsh shell prompt.
/* Tested on Robby Russell theme, and Python and Node programs.
- Duplicate the current theme you are using and copy it to .oh-my-zsh/custom/themes.
For example, if you're using Robby Russell, the custom file should also be robbyrussell.zsh-theme.
- Paste the following code at the end of the custom file to override some parts of the current them.e
*/
function program_prompt_version() {
# $1 is the name of the program, e.g. python, node.
# $2 is the command used to get the version of the program. For Node, it's `-v`. For Python, it's `-V`.
if which $1 &> /dev/null; then
local version=$($1 -$2)
# Running `python -V` will output something like `python 3.8.2` so we need to strip out the first word.
if [ "$1" = "python" ]; then
version=$(echo $version | awk -F" " '{print $NF}')
fi
echo "%{$fg_bold[blue]%}$1(%{$fg[red]%}$version%{$fg[blue]%}) %{$reset_color%}"
fi
}
last_two_dir='%S%F{cyan}%2~%f%s'
newline=$'\n👉 '
PROMPT='%{$last_two_dir%} $(program_prompt_version node v)$(program_prompt_version python V) $(git_prompt_info)${newline}'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
@bytrangle
Copy link
Author

bytrangle commented Jun 26, 2021

What the shell prompt should look like

shell-prompt-node-python-version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment