5

すべてのエイリアスが使用されているときに、それらによって参照される実際のコマンドを 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 に追加できるコマンドは 1 つありますか? すべてのエイリアスを変更する必要はありません。

4

2 に答える 2

9

エイリアスがコマンドラインの最初の単語である場合にエイリアスを表示しても問題ない場合は、次のコードを .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 zshbuiltins( emulatewhencelocal)、man zshzle( zle$BUFFER)、man zshparam( )内の詳細情報${(z)}

于 2012-02-15T21:56:11.243 に答える
0

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=$'\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: 急遽開発されたものです。確かに、それは改善することができます。

于 2020-05-19T11:34:57.987 に答える