Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh `history n` displays the first n entries instead of the last n entries

Tags:

zsh

I read the doc for history and it says:

  history [n]
  history -c
  history -d offset
  history -anrw [filename]
  history -p arg [arg ...]
  history -s arg [arg ...]
          With no options, display the command history list with line
          numbers.  Lines listed with a *  have  been  modified.   An
          argument  of  n  lists only the last n lines.  

But in my zsh, it always displays the first n lines. I need to use history | tail -n to show the last few lines.

  1. Is there any option set wrongly? How to correct it?
  2. Moreover, how to print history without line number and time stamp so that I can convert a portion of the history to a script
like image 235
doraemon Avatar asked Jan 21 '26 14:01

doraemon


1 Answers

The documentation for zsh history, in zshbuiltins(1), is:

history

Same as fc -l.

where fc -l is:

fc -l [ -LI ] [ -nrdfEiD ] [ -t timefmt ] [ -m match ] [ old=new ... ] [ first [ last ] ]

If first is not specified, it will be set to -1 (the most recent event), or to -16 if the -l flag is given. If last is not specified, it will be set to first, or to -1 if the -l flag is given. However, if the current event has added entries to the history with print -s or fc -R, then the default last for -l includes all new history entries since the current event began.

When the -l flag is given, the resulting events are listed on standard output.

so run:

history -<n>

with a hyphen-minus, as in history -16, to see the last n lines of history in zsh.

like image 58
Ry- Avatar answered Jan 23 '26 19:01

Ry-



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!