2012-02-15 50 views
2

是否有可能强制zsh在使用时回应所有别名引用的实际命令?回复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让这种情况发生的所有别名?我宁愿不必修改每个别名。

+0

'set -vx; myAlias args; set + vx'不会给你wnat(不包括'execution:'前缀?)祝你好运 – shellter 2012-02-15 19:07:20

+0

不幸的是,我正在寻找一些我可以设置的东西,一旦适用于所有别名而不与它们耦合。另外,即使我为每个别名都做了这些,它仍然会转储大量有关用于创建自定义zsh提示符的别名和函数的信息。感谢评论,不过我非常感谢帮助。 – 2012-02-15 20:29:29

回答

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 zshbuiltinsemulate

更多信息,whencelocal),man zshzlezle$BUFFER),man zshparam${(z)})。

+0

伟大的工作!我已经接受了这个答案,但是如果你能够通过它,我也会喜欢它。我不确定你是如何做到这一点的,但是我很乐意学习,不管怎样,谢谢 – 2012-02-17 00:31:39

+0

超级超级谢谢你的解释!他们非常感谢。:-) – 2012-02-18 05:29:26