在zsh中取消完成,但仅完成


13

当完成功能需要很长时间时,我可以通过按Ctrl+ C(终端中断键,发送SIGINT)或Ctrl+ G(绑定到send-break来中断它。然后,我留下未完成的单词。

但是,如果我恰好在完成功能完成时按Ctrl+ CCtrl+ G,则按键可能会取消命令行并给我一个新的提示,而不是取消完成。

如何设置zsh,以便某个键将取消正在进行的完成,但是如果没有完成功能处于活动状态,则什么也不做?

Answers:


5

这是一个设置SIGINT处理程序的解决方案,该处理程序在激活完成时仅使Ctrl+ C中断。

# A completer widget that sets a flag for the duration of
# the completion so the SIGINT handler knows whether completion
# is active. It would be better if we could check some internal
# zsh parameter to determine if completion is running, but as 
# far as I'm aware that isn't possible.
function interruptible-expand-or-complete {
    COMPLETION_ACTIVE=1

    # Bonus feature: automatically interrupt completion
    # after a three second timeout.
    # ( sleep 3; kill -INT $$ ) &!

    zle expand-or-complete

    COMPLETION_ACTIVE=0
}

# Bind our completer widget to tab.
zle -N interruptible-expand-or-complete
bindkey '^I' interruptible-expand-or-complete

# Interrupt only if completion is active.
function TRAPINT {
    if [[ $COMPLETION_ACTIVE == 1 ]]; then
        COMPLETION_ACTIVE=0
        zle -M "Completion canceled."            

        # Returning non-zero tells zsh to handle SIGINT,
        # which will interrupt the completion function. 
        return 1
    else
        # Returning zero tells zsh that we handled SIGINT;
        # don't interrupt whatever is currently running.
        return 0
    fi
}

0

我不知道这是否是可以接受的解决方案,但是发送SIGSTOP(Ctrl+ S)似乎达到了预期的效果,另外的好处是,如果在键入之前发送SIGSTART(Ctrl+ Q),则可以再次启动自动完成功能还要别的吗。我不是工作控制方面的专家,因此这可能会导致与已停止工作有关的其他混乱情况。


1
Ctrl + S和Ctrl + Q是流控制命令。它们仅影响到终端的输出,这与此处无关。可以预见,Ctrl + S在完成期间不会起作用(zsh仍然会禁用终端设置,它将获取组合键)。按Ctrl + Z(发送SIGTOP)也不起作用。
吉尔(Gilles)“所以,别再邪恶了”

谢谢!我不确定背景到底是怎么回事。我所看到的只是我控制了终端。
亚伦·冈野
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.