回显zsh中的所有别名

时间:2012-02-15 18:51:24

标签: 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.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中以使所有别名都能发生这种情况?我宁愿不必修改每个别名。

2 个答案:

答案 0 :(得分:6)

如果你知道别名是命令行中出现的第一个单词,你可以显示别名,你可以尝试将以下代码放入你的.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

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

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 zshbuiltinsemulatewhencelocal),man zshzlezle$BUFFER)中的更多信息, man zshparam${(z)})。

答案 1 :(得分:0)

感谢ZyX。我修改了his answer以使其也适用于函数。在这里;

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

PS:它是在短期内开发的。当然可以改进。