回显 zsh 中的所有别名

发布于 2025-01-05 11:00:27 字数 706 浏览 1 评论 0原文

是否可以强制 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

是否有一个命令可以添加到我的 zshrc 中以使所有别名都发生这种情况?我不想修改每个别名。

Is it possible to force zsh to echo the actual commands referred to by all aliases when they are used?

For example, say that I have the following aliases set:

# List direcory contents
alias lsa='ls -lah'
alias l='ls -la'
alias ll='ls -l'

When I execute them I would like to see each of them print the actual command that's executed. For example, I would like to see the following:

$ 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

Rather than the following:

$ 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

Is there one command that I can add to my zshrc to get this to happen for all aliases? I would prefer not to have to modify every alias.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2025-01-12 11:00:27

如果您愿意显示别名(如果别名是命令行上出现的第一个单词),您可以尝试将以下代码放入您的 .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=

描述(跳过一些琐碎的事情):

emulate -L zsh # Reset some options to zsh defaults (locally).
               # Makes function immune to user setup.

local -a WORDS # Declare WORDS as an array local to function

${(z)VARNAME}  # Split VARNAME using command-line parser.
               # Things like “"first word" "second word"” get split into 2 words:
               # “"first word"” “"second word"”

$BUFFER        # Variable containing the whole command-line. Can be modified

local -r V     # Declare variable “V” as read-only


更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[32m' RESET_COLORS=

描述(跳过一些琐碎的事情):


更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

描述(跳过一些琐碎的事情):


更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

描述(跳过一些琐碎的事情):


更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[32m' # Escape code for green foreground color in most terminals

更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[32m' RESET_COLORS=

描述(跳过一些琐碎的事情):


更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

描述(跳过一些琐碎的事情):


更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

描述(跳过一些琐碎的事情):

更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[0m' # Sequence that being echoed to terminal clears out color information whence -w cmd # Display type of the command in format “cmd: type” whence cmd # If “cmd” is an alias, then this command outputs alias value zle .accept-line # Call internal zle “accept-line” widget. This must be done or # every command will turn to no-op. You can, of course, replace # this with “eval $BUFFER” but I can’t say what will break in this case zle -N accept-line _-accept-line # Associate widget “accept-line” with function # “_-accept-line”. This makes this function responsible for accepting # lines.

更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[32m' RESET_COLORS=

描述(跳过一些琐碎的事情):

更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

描述(跳过一些琐碎的事情):

更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

描述(跳过一些琐碎的事情):

更多信息参见 man zshbuiltins< /code> (模拟从何而来本地)、man zshzle (zle , $BUFFER), man zshparam (${(z)})。

If you are fine with having aliases displayed if alias is the first word present on the command-line you can try to put the following code into your .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=

Description (some trivial things skipped):

emulate -L zsh # Reset some options to zsh defaults (locally).
               # Makes function immune to user setup.

local -a WORDS # Declare WORDS as an array local to function

${(z)VARNAME}  # Split VARNAME using command-line parser.
               # Things like “"first word" "second word"” get split into 2 words:
               # “"first word"” “"second word"”

$BUFFER        # Variable containing the whole command-line. Can be modified

local -r V     # Declare variable “V” as read-only


More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[32m' RESET_COLORS=

Description (some trivial things skipped):


More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

Description (some trivial things skipped):


More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

Description (some trivial things skipped):


More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[32m' # Escape code for green foreground color in most terminals

More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[32m' RESET_COLORS=

Description (some trivial things skipped):


More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

Description (some trivial things skipped):


More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

Description (some trivial things skipped):

More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[0m' # Sequence that being echoed to terminal clears out color information whence -w cmd # Display type of the command in format “cmd: type” whence cmd # If “cmd” is an alias, then this command outputs alias value zle .accept-line # Call internal zle “accept-line” widget. This must be done or # every command will turn to no-op. You can, of course, replace # this with “eval $BUFFER” but I can’t say what will break in this case zle -N accept-line _-accept-line # Associate widget “accept-line” with function # “_-accept-line”. This makes this function responsible for accepting # lines.

More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[32m' RESET_COLORS=

Description (some trivial things skipped):

More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

Description (some trivial things skipped):

More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

\n'"${GREEN}Executing $(whence $FIRSTWORD)${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

Description (some trivial things skipped):

More info in man zshbuiltins (emulate, whence, local), man zshzle (zle, $BUFFER), man zshparam (${(z)}).

无远思近则忧 2025-01-12 11:00:27

感谢 ZyX。我修改了他的答案,使其也适用于函数。这里是;

_-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=

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

\e[32m' RESET_COLORS=

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

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

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

\n'"${GREEN}Executing -> ${$(which $FIRSTWORD)//"$FIRSTWORD: aliased to "/""}${RESET_COLORS}" [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: function" ]] && echo -nE

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

\n'"${GREEN}Executing -> ${$(which $FIRSTWORD)//"$FIRSTWORD () { "/""}${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

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

Thanks to ZyX. I modified his answer to making it work for functions as well. Here it is;

_-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=

PS: It is developed at short notice. For sure, it can be improved.

\e[32m' RESET_COLORS=

PS: It is developed at short notice. For sure, it can be improved.

\e[0m' [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: alias" ]] && echo -nE

PS: It is developed at short notice. For sure, it can be improved.

\n'"${GREEN}Executing -> ${$(which $FIRSTWORD)//"$FIRSTWORD: aliased to "/""}${RESET_COLORS}" [[ "$(whence -w $FIRSTWORD 2>/dev/null)" == "${FIRSTWORD}: function" ]] && echo -nE

PS: It is developed at short notice. For sure, it can be improved.

\n'"${GREEN}Executing -> ${$(which $FIRSTWORD)//"$FIRSTWORD () { "/""}${RESET_COLORS}" zle .accept-line } zle -N accept-line _-accept-line

PS: It is developed at short notice. For sure, it can be improved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文