fish shell 介绍及配置

https://github.com/fish-shell/fish-shell

TODO:

好用的 fisher

https://github.com/jorgebucaran/fisher

TODO:

好用的 fish extensions

https://github.com/jethrokuan/z

纯 fish 实现的 z (快速跳转).

TODO:

个人常用 fish functions 分享

快捷的 temrinal 临时代理配置

function proxy --description 'Manage HTTP/HTTPS proxy settings'
    # 默认代理配置
    set -l default_proxy "http://127.0.0.1:7897"
    
    # 解析参数
    argparse 'h/help' 'u/unset' 's/show' 'p/port=' 'H/host=' 't/test' -- $argv
    or return 1
    
    # 显示帮助信息
    if set -q _flag_help
        echo "Usage: proxy [OPTIONS] [PROXY_URL]"
        echo ""
        echo "Manage HTTP/HTTPS proxy settings"
        echo ""
        echo "Options:"
        echo "  -u, --unset          Unset proxy environment variables"
        echo "  -s, --show           Show current proxy configuration"
        echo "  -H, --host HOST      Set proxy host (default: 127.0.0.1)"
        echo "  -p, --port PORT      Set proxy port (default: 7897)"
        echo "  -t, --test           Test proxy connection after setup"
        echo "  -h, --help           Show this help message"
        echo ""
        echo "Examples:"
        echo "  proxy                                    # Set default proxy (127.0.0.1:7897)"
        echo "  proxy --test                             # Set default proxy and test connection"
        echo "  proxy http://proxy.example.com:8080      # Set custom proxy URL"
        echo "  proxy -H 192.168.1.1 -p 8080 --test     # Set proxy with host/port and test"
        echo "  proxy --show                             # Show current proxy settings"
        echo "  proxy --unset                            # Remove proxy settings"
        return 0
    end
    
    # 显示当前代理配置
    if set -q _flag_show
        echo "Current Proxy Configuration:"
        if set -q http_proxy; or set -q https_proxy
            echo "  HTTP Proxy:  "(set -q http_proxy; and echo $http_proxy; or echo "(not set)")
            echo "  HTTPS Proxy: "(set -q https_proxy; and echo $https_proxy; or echo "(not set)")
            if set -q no_proxy
                echo "  No Proxy:    $no_proxy"
            end
        else
            echo "  No proxy configured"
        end
        return 0
    end
    
    # 取消代理设置
    if set -q _flag_unset
        set -e http_proxy
        set -e https_proxy
        set -e HTTP_PROXY
        set -e HTTPS_PROXY
        echo "✓ Proxy settings cleared"
        return 0
    end
    
    # 构建代理URL
    set -l proxy_url $default_proxy
    
    # 使用host和port参数
    if set -q _flag_host; or set -q _flag_port
        set -l host (set -q _flag_host; and echo $_flag_host; or echo "127.0.0.1")
        set -l port (set -q _flag_port; and echo $_flag_port; or echo "7897")
        
        # 验证端口号
        if not string match -qr '^\d+$' $port; or test $port -lt 1 -o $port -gt 65535
            echo "Error: Invalid port number '$port'. Must be 1-65535" >&2
            return 1
        end
        
        set proxy_url "http://$host:$port"
    else if test (count $argv) -ge 1
        set proxy_url $argv[1]
    end
    
    # 验证代理URL格式
    if not string match -qr '^https?://' $proxy_url
        echo "Warning: Proxy URL should start with http:// or https://"
        set proxy_url "http://$proxy_url"
    end
    
    # 提取主机和端口用于显示
    set -l proxy_host ""
    set -l proxy_port ""
    
    # 使用更精确的正则表达式解析URL
    if string match -qr '^https?://([^:/]+)(?::(\d+))?(?:/.*)?$' $proxy_url
        set proxy_host (string replace -r '^https?://([^:/]+)(?::(\d+))?(?:/.*)?$' '$1' $proxy_url)
        set proxy_port (string replace -r '^https?://([^:/]+)(?::(\d+))?(?:/.*)?$' '$2' $proxy_url)
    end
    
    # 设置环境变量(同时设置大小写版本以确保兼容性)
    set -gx http_proxy $proxy_url
    set -gx https_proxy $proxy_url
    set -gx HTTP_PROXY $proxy_url
    set -gx HTTPS_PROXY $proxy_url
    
    # 成功提示
    echo "✓ Proxy configured successfully"
    echo "  URL: $proxy_url"
    echo "  Host: $proxy_host"
    if test -n "$proxy_port"
        echo "  Port: $proxy_port"
    end
    
    # 仅在指定 --test 参数时测试代理连接
    if set -q _flag_test
        if command -q curl
            echo "Testing proxy connection..."
            # 使用Fish内置的方式处理超时,而不是依赖timeout命令
            if curl -s --max-time 5 --proxy $proxy_url http://httpbin.org/ip >/dev/null 2>&1
                echo "✓ Proxy connection test successful"
            else
                echo "⚠ Proxy connection test failed (proxy may be down or require authentication)"
            end
        else
            echo "⚠ curl command not found, cannot test proxy connection"
        end
    end
    
    # 提示常用的no_proxy设置
    if not set -q no_proxy
        echo ""
        echo "Tip: You may want to set no_proxy for local addresses:"
        echo "  set -gx no_proxy localhost,127.0.0.1,::1,.local"
    end
end

快捷的 terminal 临时配置 Claude Code

function setupClaudeCode --description 'Configure Claude Code proxy with token and API endpoint'
    # 默认配置
    set -l default_token "[your api token]"
    set -l default_base_url "[your api endpoint]"
    
    # 解析参数
    argparse 'h/help' 'c/clear' 'token=' 'u/url=' 's/show' 't/test' -- $argv
    or return 1
    
    # 显示帮助信息
    if set -q _flag_help
        echo "Usage: setupClaudeCode [OPTIONS]"
        echo ""
        echo "Configure Claude Code proxy settings"
        echo ""
        echo "Options:"
        echo "  --token TOKEN        Set API token (default: masked)"
        echo "  -u, --url URL        Set base URL (default: $default_base_url)"
        echo "  -s, --show           Show current configuration"
        echo "  -c, --clear          Clear all Claude Code environment variables"
        echo "  -t, --test           Test connection after configuration"
        echo "  -h, --help           Show this help message"
        echo ""
        echo "Examples:"
        echo "  setupClaudeCode                           # Use default settings"
        echo "  setupClaudeCode --token your_token        # Set custom token"
        echo "  setupClaudeCode --token token -u https://api --test  # Set both and test"
        echo "  setupClaudeCode --show                    # Show current config"
        echo "  setupClaudeCode --clear                   # Clear configuration"
        return 0
    end
    
    # 显示当前配置
    if set -q _flag_show
        echo "Current Claude Code Configuration:"
        if set -q ANTHROPIC_AUTH_TOKEN
            echo "  ANTHROPIC_AUTH_TOKEN: "(string sub -l 8 $ANTHROPIC_AUTH_TOKEN)"..."
        else
            echo "  ANTHROPIC_AUTH_TOKEN: (not set)"
        end
        echo "  ANTHROPIC_BASE_URL: "(set -q ANTHROPIC_BASE_URL; and echo $ANTHROPIC_BASE_URL; or echo "(not set)")
        return 0
    end
    
    # 清除配置
    if set -q _flag_clear
        set -e ANTHROPIC_AUTH_TOKEN
        set -e ANTHROPIC_BASE_URL
        echo "✓ Claude Code environment variables cleared"
        return 0
    end
    
    # 设置变量值
    set -l api_token $default_token
    set -l base_url $default_base_url
    
    # 使用命令行参数覆盖默认值
    if set -q _flag_token
        set api_token $_flag_token
    else if test (count $argv) -ge 1
        set api_token $argv[1]
    end
    
    if set -q _flag_url
        set base_url $_flag_url
    else if test (count $argv) -ge 2
        set base_url $argv[2]
    end
    
    # 验证输入
    if test -z "$api_token"
        echo "Error: API token cannot be empty" >&2
        return 1
    end
    
    if test -z "$base_url"
        echo "Error: Base URL cannot be empty" >&2
        return 1
    end
    
    # 验证URL格式
    if not string match -q "http*" $base_url
        echo "Warning: Base URL should start with http:// or https://" >&2
    end
    
    # 设置环境变量
    set -gx ANTHROPIC_AUTH_TOKEN $api_token
    set -gx ANTHROPIC_BASE_URL $base_url
    
    # 成功提示
    echo "✓ Claude Code proxy configured successfully"
    echo "  Token(ANTHROPIC_AUTH_TOKEN): "(string sub -l 8 $api_token)"..."
    echo "  Base URL(ANTHROPIC_BASE_URL): $base_url"
    
    # 仅在指定 --test 参数时测试连接
    if set -q _flag_test
        if command -q curl
            echo "Testing connection..."
            if curl -s --max-time 5 "$base_url" >/dev/null 2>&1
                echo "✓ Connection test successful"
            else
                echo "⚠ Connection test failed (this might be normal if endpoint requires authentication)"
            end
        else
            echo "⚠ curl command not found, cannot test connection"
        end
    end
end