理系学生日記

おまえはいつまで学生気分なのか

zsh の precmd について

自分で precmd 定義するよりか、フック利用した方が良いと思う。

そもそも zsh には、precmd_functions っていう配列に関数名突っ込んでやったら、プロンプト出す前にそれらを呼び出してくれる仕組みが存在してる。man zshmisc(1) の Hook Functions っていうところ読むと良いです。

For the functions below, it is possible to define an array that has the same name as the function with `_functions' appended. Any element in such an array is taken as the name of a function to execute; it is executed in the same context and with the same arguments as the basic function.

で、さらになんだけど、zshcontrib(1) には、そのフックをもうちょっと良い感じに登録できる関数も用意されてる。

The shell function add-zsh-hook provides a simple way of adding or removing functions from the array.

というわけで、せっかく用意されているので、そういうの使ったらほうが意図がわかりやすいしカッコいいとおもう。

autoload -Uz add-zsh-hook
add-zsh-hook precmd your_function