自动激活鱼中的 conda 环境

Automatically activating conda environments in fish

这可能吗?对于 virtualenv,我曾经使用 virtualfish,但似乎没有 conda 等价物。对于 bash,我找到了 https://github.com/chdoig/conda-auto-env which also references https://github.com/sotte/conda_auto_activate

或者,在特定目录中自动运行的鱼插件也可以正常工作。

应该是可以的。受鱼文档中这一行的启发(强调我的):

自动加载函数

当 fish 遇到命令时,它会尝试自动加载该命令的函数,方法是在 ~/.[=54= 中查找 具有该命令名称的文件 ]/.


所以我们可以有一个功能来检查每个 'cd' 命令的文件夹和 运行 脚本以在适当的时候激活。如果你只有一个VE,做起来更容易。

可能的解决方法是在新目录中检查某个切换VE的脚本文件,如果存在则执行。 (那必须注意如何处理直接切换到子目录的情况。)


在 OP 的反问后更新: 这就是我的想法。假设我们将此函数复制到名为 ~/.config/fish/functions/cd.fish:

的文件中
# search for a myInit.fish file UP THE DIRECTORY TREE, starting from the current folder.
# if found, execute it.
# Intended for automatically switching to the python  virtual environment on entering the
# directories.  Can put in other initialization stuff.

function cd --description 'change directory - fish overload'

    builtin cd $param $argv

    set -l check_dir (pwd)
    # if myInit.fish is found in the home directory:
    if test -f "$check_dir/myInit.fish"
        source $check_dir/myInit.fish
        echo "executed: source $check_dir/myInit.fish"
        return
    end

    # Look up the directory tree for myInit.fish:
    set check_dir (string split -r -m 1 / $check_dir)[1]

    while test $check_dir
        if test -f "$check_dir/myInit.fish"
            source $check_dir/myInit.fish
            echo "executed: source $check_dir/myInit.fish"
            break;
        else
            set check_dir (string split -r -m 1 / $check_dir)[1]
        end  # if ... else ...
    end  # while 
end  # function

假设您想在切换到 OpenCV 目录或其子目录之一时切换到名为 "VEOpenCV" 的 VE。为此,在 OpenCV 目录中创建一个文件并将其命名为 myInit.fish 并将以下行放在那里:

activate VEOpenCV

要在切换到主目录时停用 VE,请在主目录中创建另一个 myInit.fish 文件并在其中输入 'deactivate' 命令。如果没有,就在主目录中创建一个空的myInit.fish文件来终止目录遍历。

如果您不想重载 'builtin cd',请将上述函数的名称更改为 'myCd' 或其他名称,并重命名文件以匹配该名称。然后调用

%> myCd OpenCV

而不是

%> cd OpenCV

没有广泛测试;但有限的测试成功了。如果它不起作用,post 请在此处备注。

而且,不要忘记 post debugs/improvements/bugs 和其他相关信息 - 或者您可能会在此处找到更好的解决方案!