How can I periodically display my history number on the command line?

How can I periodically display my history number on the command line? For example, instead of showing it in EVERY clue, just do it every 7 times. (I use zsh, but I think bash should be almost identical.) The problem I am facing is that% h is not evaluated until it is in the PROMPT variable, and $ HISTCMD is always evaluated like 0 for any reason. Therefore, the inclusion of such a function in my invitation fails because $ HISTCMD is always 0:

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

This can be partially remedied by repeating "% h" instead of "$ HISTCMD", but only partially.

This is further complicated by the fact that the history command does not work (it seems) in the .zshrc file, so something like this will not work:

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

(If you are using bash, change “history 1” to “history”.)

In addition, the history file cannot be used as a source of this information, because (at least since I configured the settings and I do not want to change this configuration), the history is not shared between sessions until the zsh session is closed and its history is added To my $ HISTFILE. Therefore this will not work:

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

I am on the verge of belief that this is currently not possible. I would like someone to prove that I am wrong.

+4
source share
1 answer

You just need to postpone the evaluation of the invitation until it is issued. Just change the double quotes to single quotes:

 PROMPT='$(prompt_history) blah-blah >:' 
+4
source

Source: https://habr.com/ru/post/1333731/


All Articles