Car*_*iz 6 linux git bash zsh anaconda
如何设置PS1使git和conda都可以在bash中显示? 带有 virtualenv 和 git 分支的 Bash 命令提示符
我找到了以下关于如何在bash shell 中执行此操作的信息,但我在 macOS 中使用zsh。使用 zsh 会以同样的方式工作吗?我的 ~/.zshrc 中有以下内容
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/carlos/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/blah/profile.d/conda.sh" ]; then
. "/blah/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/blah/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
Run Code Online (Sandbox Code Playgroud)
我尝试使用此链接https://medium.com/pareture/simplest-zsh-prompt-configs-for-git-branch-name-3d01602a6f33,但它不包含 git 分支信息。有人可以对此进行扩展,以便包含 git 分支信息吗?
已安装 conda 后,在终端中运行以下命令:
conda config --set changeps1 false
Run Code Online (Sandbox Code Playgroud)
或者直接添加到文件changeps1: false中~\.condarc。这将禁用 conda 对提示符的自动修改。
接下来,使用任何文本编辑器打开~\.zshrc,并在文件底部添加以下代码:
# Determines prompt modifier if and when a conda environment is active
precmd_conda_info() {
if [[ -n $CONDA_PREFIX ]]; then
if [[ $(basename $CONDA_PREFIX) == "anaconda3" ]]; then
# Without this, it would display conda version. Change to miniconda3 if necessary
CONDA_ENV="(base) "
else
# For all environments that aren't (base)
CONDA_ENV="($(basename $CONDA_PREFIX)) "
fi
# When no conda environment is active, don't show anything
else
CONDA_ENV=""
fi
}
# Display git branch
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
# Run the previously defined function before each prompt
precmd_functions+=(precmd_conda_info)
# Define colors
COLOR_CON=$'%F{141}'
COLOR_DEF=$'%f'
COLOR_USR=$'%F{247}'
COLOR_DIR=$'%f'
COLOR_GIT=$'%F{215}'
# Allow substitutions and expansions in the prompt
setopt prompt_subst
PROMPT='${COLOR_CON}$CONDA_ENV${COLOR_USR}%n ${COLOR_DIR}%1~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}$ '
Run Code Online (Sandbox Code Playgroud)
在终端中,运行source ~/.zshrc
修改conda env提示显示设置的原始代码: 如何在zsh中修改anaconda环境提示?
显示 git 分支的原始代码: 将 Git 分支名称添加到终端提示符
| 归档时间: |
|
| 查看次数: |
715 次 |
| 最近记录: |