将 URL 解析为命令
Parse URL as a Command
当我在终端中输入 git 回购 url 时,它有以下输出:
% 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
我该如何解决这个问题。
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。
当我在终端中输入 git 回购 url 时,它有以下输出:
% 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
我该如何解决这个问题。
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。