Command line 回显zsh中的所有别名

Command line 回显zsh中的所有别名,command-line,zsh,zshrc,Command Line,Zsh,Zshrc,在使用所有别名时,是否可以强制zsh回显所有别名引用的实际命令 例如,假设我设置了以下别名: # List direcory contents alias lsa='ls -lah' alias l='ls -la' alias ll='ls -l' 当我执行它们时,我希望看到它们每个都打印实际执行的命令。例如,我希望看到以下内容: $ ll executing: 'ls -l' total 0 -rw-r--r-- 1 person staff 0 Feb 15 13:46 cool.

在使用所有别名时,是否可以强制zsh回显所有别名引用的实际命令

例如,假设我设置了以下别名:

# List direcory contents
alias lsa='ls -lah'
alias l='ls -la'
alias ll='ls -l'
当我执行它们时,我希望看到它们每个都打印实际执行的命令。例如,我希望看到以下内容:

$ ll
executing: 'ls -l'
total 0
-rw-r--r--  1 person  staff  0 Feb 15 13:46 cool.txt
-rw-r--r--  1 person  staff  0 Feb 15 13:46 sweet.html
-rw-r--r--  1 person  staff  0 Feb 15 13:46 test.md
$ ll
total 0
-rw-r--r--  1 person  staff  0 Feb 15 13:46 cool.txt
-rw-r--r--  1 person  staff  0 Feb 15 13:46 sweet.html
-rw-r--r--  1 person  staff  0 Feb 15 13:46 test.md
而不是以下内容:

$ ll
executing: 'ls -l'
total 0
-rw-r--r--  1 person  staff  0 Feb 15 13:46 cool.txt
-rw-r--r--  1 person  staff  0 Feb 15 13:46 sweet.html
-rw-r--r--  1 person  staff  0 Feb 15 13:46 test.md
$ ll
total 0
-rw-r--r--  1 person  staff  0 Feb 15 13:46 cool.txt
-rw-r--r--  1 person  staff  0 Feb 15 13:46 sweet.html
-rw-r--r--  1 person  staff  0 Feb 15 13:46 test.md

有没有一个命令可以添加到我的zshrc中,以使所有别名都能执行?我希望不必修改每个别名。

如果您对显示别名没问题,如果alias是命令行上出现的第一个单词,您可以尝试将以下代码放入.zshrc中:

_-accept-line () {
    emulate -L zsh
    local -a WORDS
    WORDS=( ${(z)BUFFER} )
    # Unfortunately ${${(z)BUFFER}[1]} works only for at least two words,
    # thus I had to use additional variable WORDS here.
    local -r FIRSTWORD=${WORDS[1]}
    local -r GREEN=$'\e[32m' RESET_COLORS=$'\e[0m'
    [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] &&
        echo -nE $'\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}"
    zle .accept-line
}
zle -N accept-line _-accept-line
说明(略过一些琐碎的事情):

更多信息请访问
manzshbuiltins
emulate
wherence
local
),
manzshzle
zle
$BUFFER
),
manzshparam
${(z)}
)。

多亏了。我修改了,使它也适用于函数。在这里

_-accept-line () {
    emulate -L zsh
    local -a WORDS
    WORDS=( ${(z)BUFFER} )
    # Unfortunately ${${(z)BUFFER}[1]} works only for at least two words,
    # thus I had to use additional variable WORDS here.
    local -r FIRSTWORD=${WORDS[1]}
    local -r GREEN=$'\e[32m' RESET_COLORS=$'\e[0m'
    [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] &&
        echo -nE $'\n'"${GREEN}Executing -> ${$(which $FIRSTWORD)//"$FIRSTWORD: aliased to "/""}${RESET_COLORS}"
    [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: function" ]] &&
        echo -nE $'\n'"${GREEN}Executing -> ${$(which $FIRSTWORD)//"$FIRSTWORD () {
    "/""}${RESET_COLORS}"
    zle .accept-line
}
zle -N accept-line _-accept-line

附言:它是在短时间内开发的。当然,它可以改进。

set-vx;八哥;set+vx
不能提供您所需要的(不包括'executing:'前缀?祝你好运。不幸的是,没有。我正在寻找一种可以一次性设置的东西,它可以应用于所有别名而不与它们耦合。此外,即使我对每个别名都这样做,它仍然会转储一堆关于别名和用于创建自定义zsh提示符的函数的信息。谢谢你的提示。)尽管如此,我还是非常感谢你的帮助。做得很好!我已经接受了这个答案,但如果你也能带我看一下,我会很高兴的。我不知道你是怎么做到的,但我很想学习。谢谢你。无论哪种方式!非常非常非常有用。谢谢你的解释!非常感谢他们。:-)