
以前、文字列を stderr にエコーする Bash スクリプト スニペットを見つけました。
echoerr() { echo "$@" 1>&2; }
echoerr hello world
これはクリップボードに残っていたので、ファイルを編集したいとき(VIM を使用)、ファイル名の代わりに誤ってこのスニペットを再度貼り付けてしまいました。
vim echoerr() { echo "$@" 1>&2; }
echoerr hello world
echoerr
以下に再割り当てされたようですvim
:
$ where vim
vim () {
echo "$@" 1>&2;
}
/usr/bin/vim
また、VIM でファイルを開こうとすると、ファイル名がそのまま返されるようになりました。
vim path/to/some-file
プリント:
path/to/some-file
どうしたの?(私はtmux内でzshを実行しています)
答え1
zsh
複数の名前で関数を定義できるためですman zshmisc
。
function word ... [ () ] [ term ] { list }
word ... () [ term ] { list }
word ... () [ term ] command
where term is one or more newline or ;. Define a function which
is referenced by any one of word. Normally, only one word is
provided; multiple words are usually only useful for setting
traps. The body of the function is the list between the { and
}. See the section `Functions'.
答え2
という関数を作成できましたvim()
。これは、zsh では 1 つの関数に複数の名前を同時に作成できるため可能です。
% vim dud() { echo ran dud function }
% dud
ran dud function
% vim path/to/some-file
ran dud function
vim()
とdud()
の両方が関数として設定されていることに注意してください。
次のように関数 def を設定解除することで、誤った関数を削除できます。
% unset -f vim
これでvim path/to/some-file
エディターが開きます。