Function 如何在不直接删除函数文件的情况下从fishshell中删除函数?

Function 如何在不直接删除函数文件的情况下从fishshell中删除函数?,function,fish,Function,Fish,我在fishshell中定义了一个函数hello: function hello echo Hello end 并保存它: funcsave hello 如果我想删除它,我可以删除文件~/.config/fish/functions/hello.fish 还有别的办法吗?(如内置的funcdel或funcrm)否,没有任何内置项可删除文件,但您可以使用: functions --erase hello 或 从当前会话中删除函数定义 另见 上述函数的解决方案-e hello仅删除

我在fishshell中定义了一个函数
hello

function hello
    echo Hello
end
并保存它:

funcsave hello
如果我想删除它,我可以删除文件
~/.config/fish/functions/hello.fish


还有别的办法吗?(如内置的
funcdel
funcrm

否,没有任何内置项可删除文件,但您可以使用:

functions --erase hello

从当前会话中删除函数定义

另见

上述
函数的解决方案-e hello
仅删除当前会话中的
hello
。打开另一个终端,功能仍然存在


要以持久的方式删除函数,我必须直接删除文件
~/.config/fish/functions/hello.fish
。到目前为止,我还不知道还有哪种方法可以持久地进行删除。

我为此创建了另一个fish函数

function funcdel
    if test -e ~/.config/fish/functions/$argv[1].fish
        rm ~/.config/fish/functions/$argv[1].fish
        echo 'Deleted function ' $argv[1]
    else
        echo 'Not found function ' $argv[1]
    end
end

受@Kanzee的答案启发(复制到文件
~/.config/fish/functions/funcdel.fish
)的一个更完整、自定义(且安静)的自制解决方案:


我将@hoijui的答案与函数funcsave中的一些代码结合起来,因此您可以一次删除多个函数:

function funcdel --description 'Deletes a fish function both permanently and from memory'
    set cf (status function)
    set -l options 'h/help'
    argparse -n funcdel $options -- $argv
    or return

    # should create a manpage
    if set -q _flag_help
        __fish_print_help cf
        return 0
    end

    if not set -q argv[1]
        printf (_ "%ls: Expected at least %d args, got only %d\n") $cf 1 0
        return 1
    end

    set -l retval 0
    for funcname in $argv
        set -l funcfile $__fish_config_dir/functions/$funcname.fish

        # Delete the in-memory function, if it exists
        functions --query -- $funcname
        if test $status -eq 0
            functions --erase -- $funcname
            printf (_ "%s: function %s removed from session\n") $cf $funcname
        else
            printf (_ "%s: Unknown function '%s'\n") $cf $funcname
        end

        # Delete the function permanently,
        # if it exists as a file in the regular location
        if test -e $funcfile
            rm $funcfile
            printf (_ "%s: %s deleted\n") $cf $funcfile
        else
            printf (_ "%s: %s not found\n") $cf $funcfile
        end
    end

    return $retval
end

谢谢我不知道为什么其他地方没有提到这一点?
function funcdel --description 'Deletes a fish function both permanently and from memory'
    set -l fun_name $argv[1]
    set -l fun_file ~/.config/fish/functions/$fun_name.fish

    # Delete the in-memory function, if it exists
    functions --erase $fun_name

    # Delete the function permanently,
    # if it exists as a file in the regular location
    if test -e $fun_file
        rm $fun_file
    end
end
function funcdel --description 'Deletes a fish function both permanently and from memory'
    set cf (status function)
    set -l options 'h/help'
    argparse -n funcdel $options -- $argv
    or return

    # should create a manpage
    if set -q _flag_help
        __fish_print_help cf
        return 0
    end

    if not set -q argv[1]
        printf (_ "%ls: Expected at least %d args, got only %d\n") $cf 1 0
        return 1
    end

    set -l retval 0
    for funcname in $argv
        set -l funcfile $__fish_config_dir/functions/$funcname.fish

        # Delete the in-memory function, if it exists
        functions --query -- $funcname
        if test $status -eq 0
            functions --erase -- $funcname
            printf (_ "%s: function %s removed from session\n") $cf $funcname
        else
            printf (_ "%s: Unknown function '%s'\n") $cf $funcname
        end

        # Delete the function permanently,
        # if it exists as a file in the regular location
        if test -e $funcfile
            rm $funcfile
            printf (_ "%s: %s deleted\n") $cf $funcfile
        else
            printf (_ "%s: %s not found\n") $cf $funcfile
        end
    end

    return $retval
end