代码之家  ›  专栏  ›  技术社区  ›  Ahmad Ismail

将URL解析为命令

zsh
  •  0
  • Ahmad Ismail  · 技术社区  · 6 年前

    % https://github.com/chmln/sd.git        
    zsh: no such file or directory: https://github.com/chmln/sd.git
    

    我想要 https://github.com/chmln/sd.git

    git clone https://github.com/chmln/sd.git
    cd sd
    

    我试过了 preexec 钩子。

    preexec () { 
        print ">>>preexec start<<<"
        # print -l ${(qqq)@}
    
        if  [[ ${(qqq)@} =~ ^https.* ]]
        then
        echo URL
        fi
        print ">>>preexec end<<<"
    
    }
    

    输出是

    % https://github.com/chmln/sd.git
    >>>preexec start<<<
    >>>preexec end<<<
    zsh: no such file or directory: https://github.com/chmln/sd.git
    

    0 回复  |  直到 6 年前
        1
  •  0
  •   Ahmad Ismail    6 年前
    function _accept-line-with-url {
    
        if  [[ $BUFFER =~ ^https.*git ]]
        then
    
            echo $BUFFER >> $HISTFILE
            fc -R
    
            BUFFERz="git clone $BUFFER && cd $(basename $BUFFER .git)"
            zle .kill-whole-line
            BUFFER=$BUFFERz
            zle .accept-line
        else 
            zle .accept-line
        fi 
    }
    
    zle -N accept-line _accept-line-with-url
    

    现在您可以在提示符中粘贴github url,它将克隆repo。