1
1

More than 1 year has passed since last update.

Zshでコマンドを実行したらプロンプトを書き換える

Posted at

やりたいこと

プロンプトの右側にコマンドを実行した時刻を残したい。でも、現在のプロンプトには時刻の代わりに、gitのブランチとかの情報を表示したい。

以下のような感じで
2110061950.gif

解決策

precmdフックとaccept-lineを使って、RPROMPT変数を交互に書き換えてやるとよいです。

.zshrc
# setopt等は適当にやってある

# 現在のプロンプトにはVCSの情報を表示
current-rprompt() {
  RPROMPT='${vcs_info_msg_0_}'
}
add-zsh-hook precmd current-rprompt

# 以前のプロンプトにはコマンドラインを確定した時刻を表示
update-rprompt-accpet-line() {
  RPROMPT='%D{%y/%m/%d %H:%M:%S}'
  zle .reset-prompt
  zle .accept-line
}
zle -N accept-line update-rprompt-accpet-line
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1