【问题标题】:zsh history is too shortzsh 历史太短
【发布时间】:2015-01-06 22:19:32
【问题描述】:

当我在 Bash 中运行 history 时,我得到了大量结果 (1000+)。但是,当我运行history zsh shell 时,我只得到 15 个结果。这使得 zsh 中的 grepping 历史几乎毫无用处。 我的.zshrc 文件包含以下几行:

HISTFILE=~/.zhistory
HISTSIZE=SAVEHIST=10000
setopt sharehistory
setopt extendedhistory

如何修复 zsh 以使我的 shell 历史记录更有用?


更新

如果在 zsh 中调用 history 1,我将获得所有历史记录,就像在 Bash 中调用 history 一样。我可以为命令设置别名以获得相同的结果,但我想知道为什么 history 在 zsh 和 Bash 中的行为不同。

【问题讨论】:

  • 作为 grepping 历史记录的替代方法,我强烈推荐 fzf (github.com/junegunn/fzf) - 它允许在 ctrl+r 上的 shell 历史记录中进行模糊搜索。
  • 简洁明了。感谢您提出问题并提供明确的答案!

标签: zsh history


【解决方案1】:
#set history size
export HISTSIZE=10000
#save history after logout
export SAVEHIST=10000
#history file
export HISTFILE=~/.zhistory
#append into history file
setopt INC_APPEND_HISTORY
#save only one command if 2 common are same and consistent
setopt HIST_IGNORE_DUPS
#add timestamp for each entry
setopt EXTENDED_HISTORY   

这是我的设置,它有效

【讨论】:

  • +1 表示 有用的设置,但它不回答问题(zsh 中的history(不带参数)仅显示 15 个最近的条目,所以问题是显示问题)。
  • 仅供参考:bck-i-search(zsh 中的反向历史搜索)将搜索整个历史(>历史 1)
  • setopt EXTENDED_HISTORY 之后的时间戳是怎么得到的?
【解决方案2】:

NVaughan(OP)已经在问题的更新中说明了答案:history 在bash 中的行为与在zsh 中的行为不同:

简而言之:

  • zsh:
    • history 仅列出 15 个最近的 历史条目
    • history 1 列出全部 - 见下文。
  • 狂欢:
    • history 列出所有历史条目。

遗憾的是,将数字操作数传递给history 的行为也不同:

  • zsh:
    • history <n> 显示所有条目以 <n> 开头 - 因此,history 1 显示所有 条目。
    • (history -<n> - 注意- - 显示<n> 最近 条目,因此默认行为实际上是history -15)
  • 狂欢:
    • history <n> 显示<n>最近项。
    • (bash 的history 不支持列出from条目号;您可以使用fc -l <n>,但必须存在特定条目<n>,否则命令失败 - 见下文。)

可选背景信息:

  • 在 zsh 中,history 实际上是(实际上不是)fc -l 的别名:参见man zshbuiltins
    • 有关许多历史相关功能,请参阅man zshall
  • 在 bash 中,history 是它自己的命令,其语法不同于 fc -l
    • 见:man bash
  • bash 和 zsh 都支持 fc -l <fromNum> [<toNum>] 列出给定的范围历史条目:
    • bash:特定条目<fromNum> 必须存在。
    • zsh:只要至少 1 个条目在(显式或隐式)范围内,命令就会成功。
    • 因此,fc -l 1 在 zsh 中工作以返回所有历史条目,而在 bash 中它通常不会,因为条目 #1 通常不再存在(但是,如前所述,您可以使用 history 不带参数列出 bash 中的所有条目)。

【讨论】:

  • 有没有办法改变zsh的history的默认输出行数? (除了明显的alias history="fc -l $NUMBER)
【解决方案3】:

也许迟到了,但是看到这篇文章并尝试应用它,但失败了....所以实际上,把它放在.zshrc:

alias history='history 1'

在HIST_SIZE 用完之前你会看到任何东西。查找我使用的命令(.zshrc 更改后)

history | grep "my_grep_string"

【讨论】:

  • 更改命令的含义总是有点不稳定......最好定义alias hist='history 1',这样原始history的含义不受影响(以防万一是使用它的脚本等)。总的来说,我喜欢使用别名让生活更轻松的想法。
猜你喜欢
  • 2019-12-12
  • 2016-08-11
  • 2021-01-18
  • 2012-09-09
  • 2015-05-13
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多