如何在shell提示中间歇显示我的历史记录命令编号? [英] How can I intermittently show my history command number in my shell prompt?

查看:65
本文介绍了如何在shell提示中间歇显示我的历史记录命令编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的shell提示符中间歇显示我的历史记录命令编号?例如,与其在每个提示中都显示它,不如每7次执行一次.(我使用的是zsh,但我认为bash应该实际上是相同的.)我遇到的问题是,直到%h都在PROMPT变量中时才对其进行评估,并且由于某种原因,$ HISTCMD始终被评估为0.因此将这样的函数放入提示中失败,因为$ HISTCMD始终为0:

How can I intermittently show my history command number in my shell prompt? For instance, rather than showing it in EVERY prompt, just do it every 7 times. (I'm using zsh, but I think bash should be virtually identical.) The problem I encounter is that %h is not evaluated until it's in the PROMPT variable, and $HISTCMD is always evaluated as 0 for some reason. So putting a function like this into my prompt fails because $HISTCMD is always 0:

prompt_history() {
CYCLE=$(( $HISTCMD % 7 ))
if [[ "$CYCLE" = "0" ]]; then
echo -ne "$HISTCMD"
fi
}

PROMPT="$(prompt_history) blah-blah >:"

可以通过回显%h"而不是"$ HISTCMD"来部分解决此问题,但只能部分解决此问题.

This can be partly fixed by echoing "%h" instead of "$HISTCMD", but only partly.

由于 history 命令在 .zshrc 文件中不起作用(似乎)这一事实使情况变得更加复杂,因此这样的事情将不起作用:

It is further complicated by the fact that the history command does not (seem to) function within a .zshrc file, so something like this won't work:

CYCLE="$(( $(history 1 | wc -l) % 7 ))"

(如果您正在使用bash,请将历史记录1"更改为历史记录".)

(If you're using bash, change "history 1" to just "history".)

此外,历史记录文件也不能用作此信息的来源,因为(至少我已经配置了方式,并且我宁愿不更改此配置)在zsh会话关闭之前,会话之间不会共享历史并将其历史记录添加到我的$ HISTFILE中.因此,这将不起作用:

Also, the history file is not usable as a source of this information since (at least the way I have things configured--and I'd rather not change this configuration) history is not shared between sessions until a zsh session closes and its history is added to my $HISTFILE. Therefore, this won't work:

CYCLE="$(( $(cat $HISTFILE | wc -l) % 7 ))"

我几乎相信这是不可能的.我希望有人证明我做错了.

I'm on the verge of believing this is currently impossible. I'd love someone to prove me wrong.

推荐答案

您只需要延迟对提示的评估,直到其发出为止.只需将双引号更改为单引号即可:

You simply need to delay evaluation of the prompt until it's issued. Just change the double quotes to single quotes:

PROMPT='$(prompt_history) blah-blah >:'

这篇关于如何在shell提示中间歇显示我的历史记录命令编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆