Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

在 Zsh 中记录 Shell 历史

Posted on 2022-12-21

默认情况下,大多数 shell 不会以详细持久的方式记录您的历史记录,这放弃了在命令行上工作的一大优势。好的历史可以让您以可搜索和可浏览的方式回顾您几个月或几年前所做的事情,这样您就可以回答诸如“我是如何生成这个数字的?”、“我使用的那个技巧是什么?”或“如果我现在正在做类似的事情,我应该回忆上次的什么?”

我使用bash作为我的 shell 并且长期使用:

  
.bashrc:  
  promptFunc() {  
    echo "$(date +%Y-%m-%d--%H-%M-%S) $(hostname) $PWD $(history 1)" \  
      >> ~/.full_history  
  }  
  PROMPT_COMMAND=提示函数  

我向工作人员推荐这个,但他们使用zsh 。在zsh中,您可以通过设置INC_APPEND_HISTORY (立即追加到历史记录而不是等待 shell 退出)、 SAVEHIST=1000000000 (实际上不限制磁盘上的历史记录大小)和EXTENDED_HISTORY (存储带有历史条目的时间戳)。但是,如果您在没有设置这些的情况下不小心调用了zsh ,并且它不会写入您在其中运行命令的目录(这是我经常引用的元数据),那么您可能会丢失大部分历史记录。

Mike调整了我的代码片段以在zsh中运行:

  
.zshrc:  
  预命令(){  
    echo "$(日期 +%Y-%m-%d--%H-%M-%S) $(主机名) $PWD $(历史 -1)" \  
      >> ~/.full_history  
  }  

这两个更改是zsh使用precmd而不是PROMPT_COMMAND ,并且您需要history -1而不是history 1 。

您仍然可以在两者上使用相同的histgrep命令:

  
函数 histgrep {  
  本地 n_lines=10  
  如果 [[ "$1" =~ ^[0-9]*$ ]];然后  
    n_lines="$1"  
    转移  
  菲  
  grep "$@" ~/.full_history |尾 -n "$n_lines"  
}  

请注意,这不会取代您的 shell 的内置历史记录工具,我不建议将其关闭。这只是一个非常便宜的额外日志记录层,带有额外的元数据并且意外删除的风险较小。

评论来自: facebook , lesswrong , mastodon

原文: https://www.jefftk.com/p/logging-shell-history-in-zsh

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Rohit Patel
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme