0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【zsh】historyとaliasで、今日(昨日)実行した全コマンドを表示するコマンドを作った

Posted at

きっかけ・やりたいこと

  • 最近実行したコマンドから検索をしたいって場面がある。
  • 一覧で出したいので^+rはダメ。
  • ちょっと前までなら history | grep "hoge" でいいのだが、historyの表示数によってはヒットしないこともある。
  • なら、今日実行したコマンドを表示するコマンドを作って、それにgrepすればいいじゃん!
  • ついでに今日と昨日の2日間verも作るか。

結果

~/.zshrc
# 今日の全履歴
alias histt='history -i 0 | grep --color=none "$(date "+%Y-%m-%d")"'

# 今日と昨日の全履歴
alias histtt='history -i 0 | grep --color=none -e "$(date "+%Y-%m-%d")" -e "$(date -v -1d "+%Y-%m-%d")"'

# おまけ: history自体も常に時刻表示&30件表示にした。
alias hist="history -i -30"

かんたんな解説

-iオプションは、時刻表示を有効にするオプション。yyyy-mm-dd形式で出してくれる。
historyへの引数0は、0番目(つまり最初)からの履歴を出力してねという意味。$ history 0 10 みたいにすると0番目から10番目の履歴を出力してくれる。

あとはdateコマンドでいい感じの文字列を生成してgrepすれば完了。dateをちゃんと使ったことがなくてちょっと躓いた。

参考

どうせ同じだろと思ってlinux版date説明読んでたら、--dateオプションがないらしい。

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?