Frage

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
War es hilfreich?

Lösung

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/'
}

Andere Tipps

history is a synonym for fc -l, and I'm not aware of a method using fc to show what you want. It's almost certainly possible to extend zsh to provide such a command, although I'm not quite up to writing one. A simple awk command can process the output of history, though:

history | awk "{print \$1-$HISTCMD, \$0}"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top