带有目录和虚拟环境的 Zsh 提示符
Zsh prompt with directory and virtual environment
我很难找到一个简洁的答案。我正在使用 Oh My Zsh,现在使用默认主题 robbyrussell。
我希望我的提示有两个组成部分:
- 当前目录,加上上一级
- 如果我使用的是虚拟环境(如 anaconda),请在括号中加上活动环境的名称。
以下是实现方式,您可以根据自己的需要进行定制
# Helper method to add background and foreground colors
prompt_segment () {
local bg fg
[[ -n ]] && bg="%K{}" || bg="%k"
[[ -n ]] && fg="%F{}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && != $CURRENT_BG ]]
then
echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
else
echo -n "%{$bg%}%{$fg%} "
fi
CURRENT_BG=
[[ -n ]] && echo -n
}
prompt_virtualenv () {
# Check if we are in a virtual environment
# if we are then VIRTUAL_ENV variable will be set
local virtualenv_path="$VIRTUAL_ENV"
if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]
then
# We are in virtual env so show just the project name
prompt_segment blue black "(`basename $virtualenv_path`)"
fi
}
prompt_directory() {
# Show the current directory
prompt_segment red blue $PWD
}
build_my_zsh_prompt() {
# Call all the prompt functions to build the actual prompt
prompt_virtualenv
prompt_directory
prompt_segment black white ""
}
# Assign the PROMPT variable with the function, so bash call it everytime
# Single quotes are important here, else you will get a fixed PROMPT
# Without single quotes, the function will be called once and evaluated value
# will be assigned
PROMPT='$(build_my_zsh_prompt)'
PROMPT
变量被zsh
shell用来决定需要显示什么作为提示。当我们设置 PROMPT=$(build_my_zsh_prompt)
时,我们要求 shell 调用我们的函数 build_my_zsh_prompt
.
这个函数反过来(理想情况下)应该调用不同的函数来创建提示的各个部分。现在让我们看看prompt_directory
prompt_segment black red $PWD"
prompt_segment
是一个辅助函数,用于回显一些带有背景和前景色的文本
本例中第一个参数black
是背景色,第二个参数red
是前景色。接下来我们显示需要为此提示提供的文本。
所有这些都需要添加到您的 ~/.zshrc
文件的最后
我很难找到一个简洁的答案。我正在使用 Oh My Zsh,现在使用默认主题 robbyrussell。
我希望我的提示有两个组成部分:
- 当前目录,加上上一级
- 如果我使用的是虚拟环境(如 anaconda),请在括号中加上活动环境的名称。
以下是实现方式,您可以根据自己的需要进行定制
# Helper method to add background and foreground colors
prompt_segment () {
local bg fg
[[ -n ]] && bg="%K{}" || bg="%k"
[[ -n ]] && fg="%F{}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && != $CURRENT_BG ]]
then
echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
else
echo -n "%{$bg%}%{$fg%} "
fi
CURRENT_BG=
[[ -n ]] && echo -n
}
prompt_virtualenv () {
# Check if we are in a virtual environment
# if we are then VIRTUAL_ENV variable will be set
local virtualenv_path="$VIRTUAL_ENV"
if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]
then
# We are in virtual env so show just the project name
prompt_segment blue black "(`basename $virtualenv_path`)"
fi
}
prompt_directory() {
# Show the current directory
prompt_segment red blue $PWD
}
build_my_zsh_prompt() {
# Call all the prompt functions to build the actual prompt
prompt_virtualenv
prompt_directory
prompt_segment black white ""
}
# Assign the PROMPT variable with the function, so bash call it everytime
# Single quotes are important here, else you will get a fixed PROMPT
# Without single quotes, the function will be called once and evaluated value
# will be assigned
PROMPT='$(build_my_zsh_prompt)'
PROMPT
变量被zsh
shell用来决定需要显示什么作为提示。当我们设置 PROMPT=$(build_my_zsh_prompt)
时,我们要求 shell 调用我们的函数 build_my_zsh_prompt
.
这个函数反过来(理想情况下)应该调用不同的函数来创建提示的各个部分。现在让我们看看prompt_directory
prompt_segment black red $PWD"
prompt_segment
是一个辅助函数,用于回显一些带有背景和前景色的文本
本例中第一个参数black
是背景色,第二个参数red
是前景色。接下来我们显示需要为此提示提供的文本。
所有这些都需要添加到您的 ~/.zshrc
文件的最后