conda init: create separate shell configuration file
Currently, conda init hardcodes local paths into shell configuration files like .bashrc and .zshrc inside the >> conda initialize >>> block, which is managed by conda. This causes problems for those (like me) that want to sync configuration files between machines, where the hardcoded paths will often differ.
A solution would be for conda init to write the configuration block to a separate file ~/.conda_init.rc or similar. This file in turn is sourced by the general shell configuration file (.zshrc etc).
This way, the only thing added to .zshrc etc would be source ~/.conda_init.rc, which is very portable. The local conda can then update the local ~/.conda_init.rc and this file can be kept out of dot-file syncing managers.
I would really appreciate this change for the same reason as @egpbos. Has there been an progress on this?
Can someone point me to where the code would need to be updated to add this as an option? Maybe I can do the work.
Hi there, thank you for your contribution!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed automatically if no further activity occurs.
If you would like this issue to remain open please:
- Verify that you can still reproduce the issue at hand
- Comment that the issue is still reproducible and include: - What OS and version you reproduced the issue on - What steps you followed to reproduce the issue
NOTE: If this issue was closed prematurely, please leave a comment.
Thanks!
This is still a relevant issue.
The following is my solution and I hope it can help someone like me.
In my .zshrc, there is source ~/dotF/zsh/miniconda.zsh and source ~/dotF/prompt.zsh. After installing conda or miniconda on a new machine, I need not to issue conda init, which command would
annoyingly modify my personal .zshrc .
The content in miniconda.zsh is:
export CONDA_EXE="$HOME/miniconda3/bin/conda"
export _CE_M=''
export _CE_CONDA=''
export CONDA_PYTHON_EXE="$HOME/miniconda3/bin/python"
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
__conda_exe() (
"$CONDA_EXE" $_CE_M $_CE_CONDA "$@"
)
__conda_hashr() {
if [ -n "${ZSH_VERSION:+x}" ]; then
\rehash
elif [ -n "${POSH_VERSION:+x}" ]; then
: # pass
else
\hash -r
fi
}
__conda_activate() {
if [ -n "${CONDA_PS1_BACKUP:+x}" ]; then
# Handle transition from shell activated with conda <= 4.3 to a subsequent activation
# after conda updated to >= 4.4. See issue #6173.
PS1="$CONDA_PS1_BACKUP"
\unset CONDA_PS1_BACKUP
fi
\local ask_conda
ask_conda="$(PS1="${PS1:-}" __conda_exe shell.posix "$@")" || \return
\eval "$ask_conda"
__conda_hashr
}
__conda_reactivate() {
\local ask_conda
ask_conda="$(PS1="${PS1:-}" __conda_exe shell.posix reactivate)" || \return
\eval "$ask_conda"
__conda_hashr
}
conda() {
\local cmd="${1-__missing__}"
case "$cmd" in
activate|deactivate)
__conda_activate "$@"
;;
install|update|upgrade|remove|uninstall)
__conda_exe "$@" || \return
__conda_reactivate
;;
*)
__conda_exe "$@"
;;
esac
}
if [ -z "${CONDA_SHLVL+x}" ]; then
\export CONDA_SHLVL=0
# In dev-mode CONDA_EXE is python.exe and on Windows
# it is in a different relative location to condabin.
if [ -n "${_CE_CONDA:+x}" ] && [ -n "${WINDIR+x}" ]; then
PATH="$(\dirname "$CONDA_EXE")/condabin${PATH:+":${PATH}"}"
else
PATH="$(\dirname "$(\dirname "$CONDA_EXE")")/condabin${PATH:+":${PATH}"}"
fi
\export PATH
# We're not allowing PS1 to be unbound. It must at least be set.
# However, we're not exporting it, which can cause problems when starting a second shell
# via a first shell (i.e. starting zsh from bash).
if [ -z "${PS1+x}" ]; then
PS1=
fi
fi
The content in miniconda.zsh is:
# /home/wf/Man/zsh_S/PS1等prompt.mm
# ref: /home/wf/d/leo_tools/antidote_plug/woefe/git-prompt.zsh/examples/wprompt.zsh#
wf_prompt_setup() {
if [[ -n $_PROMPT_INITIALIZED ]] ; then
return
else
_PROMPT_INITIALIZED=1
# 依赖于VIRTUAL_ENV(conda不会export这个变量吧?)
# # Prevent Python virtualenv from modifying the prompt
# export VIRTUAL_ENV_DISABLE_PROMPT=1
#
# # Set $psvar[12] to the current Python virtualenv
# function _prompt_update_venv() {
# psvar[12]=
# if [[ -n $VIRTUAL_ENV ]] && [[ -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
# psvar[12]="${VIRTUAL_ENV:t}"
# fi
# }
# add-zsh-hook precmd _prompt_update_venv
# Draw a newline between every prompt
_prompt_newline(){
if [[ -z "$_PROMPT_NEWLINE" ]]; then
_PROMPT_NEWLINE=1
elif [[ -n "$_PROMPT_NEWLINE" ]]; then
echo
fi
}
add-zsh-hook precmd _prompt_newline
# To avoid glitching with fzf's alt+c binding,
# we override the fzf-redraw-prompt widget.
# The widget by default reruns all precmd hooks, which prints the newline again.
# We therefore run all precmd hooks except _prompt_newline.
# fzf-redraw-prompt()这函数定义给谁用??
# /home/linuxbrew/.linuxbrew/Cellar/fzf/0.42.0/shell/key-bindings.zsh里有fzf-file-widget, 没有fzf-redraw-prompt:
# 不过我覆盖了ctrl-t, 应该不用管?
# fzf-file-widget() {
# LBUFFER="${LBUFFER}$(__fsel)"
# local ret=$?
# zle reset-prompt
# return $ret
# }
# zle -N fzf-file-widget
# bindkey -M emacs '^T' fzf-file-widget
# bindkey -M vicmd '^T' fzf-file-widget
# bindkey -M viins '^T' fzf-file-widget
#
function fzf-redraw-prompt() {
local precmd
for precmd in ${precmd_functions:#_prompt_newline}; do
$precmd
done
zle reset-prompt
}
fi
}
wf_prompt_setup
# lwf:
# setopt PROMPT_SUBST # Allow parameter and command substitution in the prompt
# /home/wf/d/leo_tools/antidote_plug/woefe/git-prompt.zsh/examples/wprompt.zsh里设置了
# autoload -Uz promptinit; promptinit
# -U: suppress alias expansion for functions
# /home/linuxbrew/.linuxbrew/Cellar/zsh/5.9/share/zsh/functions/promptinit
# zsh prompt themes extension
# Type `prompt -h' for help.
# 不再需要promptinit了:
# Unlike other popular prompts, git-prompt.zsh does not use `promptinit`,
# which gives you the flexibility to build your own prompt from scratch.
# You can build a custom prompt by
# setting the `PROMPT` variable in your `.zshrc` after souring the `git-prompt.zsh`.
# And you should use `'$(gitprompt)'` in your `PROMPT` to get the Git prompt.
# git-prompt.zsh/git-prompt.zsh里有:
# # Disable promptinit if it is loaded
# (( $+functions[promptinit] )) && {promptinit; prompt off}
autoload -U colors ; colors
# 定义了fg bg等环境变量
# 可以用这些颜色:Red, Blue, Green, Cyan, Yellow, Magenta, Black & White
# https://void-shana.moe/linux/customize-your-zsh-prompt.html
# 暂时没复杂到 不用中文就很难辨认. 避免中文导致在新环境出bug, 这里先不用中文作为变量名
# ✗你没看错,中文能当变量✗,
# 换行=$'\n'
## 上行=$'\e[1A'
## 上行=$'\e[1B'
# PS1
cda $(cat $cache_X/conda_name)
# cda定义于 /home/wf/dotF/zsh/alias.zsh
# cda里export了PS1
if [[ "$TERM_PROGRAM" == "vscode" ]] ; then # 下面source的文件, 好像有PS1以外的功能, 但我只关心PS1
# https://code.visualstudio.com/docs/terminal/shell-integration#_portability-versus-performance
. "$HOME/dotF/zsh/vscode_shell.zsh"
# . "$(code --locate-shell-integration-path zsh)"
echo 'vscode对zsh会动手脚, 这里参考官方指示, source了shellIntegration啥的文件'
# https://code.visualstudio.com/docs/terminal/shell-integration#_manual-installation
#
# 想用vscode编辑vi模式的zsh的命令行内容, 不行?
# export EDITOR=code
# export VISUAL=code
fi
# PS1 随着vi_mode和的变化:
# todo: https://github.com/sisrfeng/zsh-vim-mode/blob/main/README.adoc里有说怎么处理KEYMAP变化
# 不过现在这样用起来也没啥问题
Vim_PS1() {
PS1[-2]="${${KEYMAP/vicmd/}/(main|viins)/}"
# 在PS1里显示vi的模式
# KEYMAP是一个parameter(记录当前为emcas还是vicmd viins等的变量)
# 这里是两层变量替换? ${原字符串/要换掉的词/用谁去换}
# ${前面的结果/(main|viins)/}
# main和viins都换成后的的字符?
zle reset-prompt
}
hooks-add-hook zle_keymap_select_hook Vim_PS1
# hooks-add-hook解释:
# wf_zle_keymap() {
# echo '123测试123'
# }
# hooks-add-hook zle_keymap_select_hook wf_zle_keymap
# hooks-add-hook zle_line_init_hook wf_zle_keymap
#
# 解释:
# hooks-add-hook来自插件https://github.com/zsh-hooks/zsh-hooks
# - zle_keymap_select_hook, 是个由函数组成的array
#
# 方便了写其他插件的人, 不覆盖用户自己的:
#
# - zle-keymap-select, 是User defined widgets里的special widgets
# 定义:
# zle-keymap-select(){
# 命令1
# 命令2
# ....
# }
# RPS1:
source $HOME/dotF/zsh/git-prompt__cfg.zsh
# You can build a custom prompt by setting the PROMPT variable in your .zshrc
# after souring the git-prompt.zsh ( source git-prompt.zsh/git-prompt.zsh, 由antidote管理)
# And you should use '$(gitprompt)' in your PROMPT to get the Git prompt.
# You must set your PROMPT with single quotes,
# not double quotes,
# otherwise the Git prompt will not update properly.
Git_Vim__rightPS1() {
# RPS1: git
# RPS1=${换行} # 加了会导致RPS1看不见
if [[ $PWD == *'final'* ]] ; then
export RPS1=
else
# export RPS1="$(git_super_status)" # 这个插件不怎么更新了, 改用git-prompt.zsh
export RPS1='$(gitprompt)'
fi
Vim_PS1
}
# 方便切换是否显示git状态:
gps(){
hooks-add-hook zle_line_init_hook Git_Vim__rightPS1
# 如果没有这行, # 敲完命令不能更新KEYMAP至正在用的vicmd或viins
# zle_line_init_hook: 每次切换路径或敲完一个命令, 都触发
}
gps0(){
export RPS1=
# 要对付上面的这行吗:
# hooks-add-hook zle_line_init_hook Git_Vim__rightPS1
}
gps # 默认显示git状态
# gps0
#
#
# #
# PS2和RPS2
PS2="%{$fg[cyan]%}%_>%{$reset_color%}"
RPS2="%{$fg[cyan]%} 换行了_继续 %{$reset_color%}"