如何将管道和命令插入到当前输入的末尾?

How to insert a pipe and command into the end of the current input?

我找到了一个很好的命令,叫做 espeak。 它可以从 stdin 读取并说出它的结果。

$ du -sh . | espeak
$ ls -a | grep foobar | espeak
etc

无论如何,它工作正常,但我认为每次都将 | espeak 放入行尾效率不高。 所以我想自动插入而不用手动输入。 有什么好的方法可以实现吗?

假设您还想查看输出,您可以这样做:

exec > >(tee >(espeak))

这会将 stdout 重定向到 tee 进程,该进程将所有内容发送到 espeak 进程,并将其发送到控制台或 stdout 正在发送的任何内容到之前。 (对于那些在家跟随的人来说,tee 进程的 stdout 在启动时还没有被重定向,所以它仍然和 exec 命令之前一样。 )

玩得开心。

要关闭它:

exec > /dev/tty

在 zsh 中有一个小部件 accept-line 当输入行被接受时触发。您可以用自己的组件覆盖默认小部件,如下所示:

function accept-line
{
    #here, add espeak to the current command
    BUFFER=$BUFFER" | espeak"

    # to be clear, if the input string was 'ls -l',
    # now it is 'ls -l | espeak', and this is the command that will be executed

    # trigger the real widget to accept the line and run the input buffer
    zle .accept-line
}
# register your new accept-line widget to replace the builtin one
zle -N accept-line

这里我使用了名称 accept-line 但如果您想将覆盖函数命名为 my_superOverride 您可以在末尾使用 :

覆盖默认小部件
zle -N accept-line my_superOverride

唯一的缺点是,对于像 do something ; do something else 这样的命令,它只会读出 do something else..

的输出