Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh: display reverse history number

Tags:

zsh

The bang history mechanism allow to specify a backward relative command, like !-2 to relaunch the penultimate command. That's fine but if you launch history, you will only have the command serial number, which depending of your history length may be far longer.

So what I would like is a way to define a command extended_history which print

-5 12342 print example
[…]
-2 12345 print foo
-1 12346 print bar
like image 234
psychoslave Avatar asked Dec 21 '25 15:12

psychoslave


1 Answers

At least on linux there a the tools tac (which prints lines in reverse order) and nl (which numbers lines).

So this should do the trick although without leading -:

extended_history () {
    history "$@" | tac | nl | tac
}

If you really want the -

extended_history () {
    history "$@" | tac | nl | tac | sed 's/^\( *\)\([0-9]\)/\1-\2/'
}
like image 184
Adaephon Avatar answered Dec 24 '25 09:12

Adaephon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!