在zsh中回显所有别名 [英] Echo all aliases in zsh
问题描述
是否可以强制zsh在使用所有别名时回显实际的命令?
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
而不是以下内容:
$ 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 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.
推荐答案
如果别名是第一个单词,则可以显示别名出现在命令行上,您可以尝试将以下代码放入.zshrc中:
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=$'\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
说明(跳过了一些琐碎的事情):
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
$'\e[32m' # Escape code for green foreground color in most terminals
$'\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
(模拟
,为什么
,本地
), man zshzle
( zle
, $ BUFFER
), man zshparam
( $ {(z)}
)。
More info in man zshbuiltins
(emulate
, whence
, local
), man zshzle
(zle
, $BUFFER
), man zshparam
(${(z)}
).
这篇关于在zsh中回显所有别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!