8

I typed in the history command and it showed me the last 10 commands executed by me. Now I wanted to see the last 20 commands executed by me so (after reading the documentation):

An argument of n lists only the last n lines.

I typed in history 20. This showed me all the commands starting from command number 20 to the current command which was somewhere around 2000. So I tried history -20 and this works. It shows me the last 20 commands. But this is not what is said in the documentation.

Also history -d [offset] is supposed to delete the command on that offset. Even that does not work in my zsh.

This is straight out of my zsh shell : enter image description here

  • "An argument of n lists only the last n lines" is in the documentation for bash. Zsh does things differently. zsh.sourceforge.net/Doc/Release/… – Mark Plotnick Aug 8 '14 at 1:20
  • On Linux, the man builtins command will show the BASH BUILTIN COMMANDS page, regardless of what shell you're using. Try man zshbuiltins or the web page in my previous comment to see the correct documentation. – Mark Plotnick Aug 13 '14 at 18:18
9

In zsh, history is an alias for fc -l 1, so when you do history -20 it get replaced by fc -l 1 -20 which just won't work, so instead use fc directly:

➜  ~  fc -l -20
10095  grep -R PAPER /usr/lib/locale/
10096  man locale
10097  man 7 locale
10098  mc
10099  history
10100  history --help
10101  run-help history
10102  history 20
10103  history 1 20
10104  history -l 20
10105  fc
10106  history -l 20
10107  type history
10108  fc -l ..20
10109  fc -l -20
10110  history -l -20
10111  history -20
10112  fc -l -20
10113  type history
10114  fc -l 1 -20

and you'll be fine.

| improve this answer | |
  • You dint' understand my question. I know that history -20 (or fc -l -20) works. I want to know why history 20 (or fc -l 20) does not work ? It should work as per the documentation. – user590849 Aug 8 '14 at 0:07
  • @user590849 run-help history doesn't say anything about that. Where did you find this documentation? – Braiam Aug 8 '14 at 0:15
  • @Braiam: history ~~ fc -l, why fc -l 1? – cuonglm Aug 8 '14 at 5:05
  • 1
    @user590849, you're reading bash documentation, not zsh documentation. – Stéphane Chazelas Aug 8 '14 at 15:20
  • Add the following to your .zshrc: function lhistory() { fc -l -$1} and use it like to show the last (for example) 10 commands: lhistory 10 – Olshansk Aug 17 '19 at 2:33
3

The first thing to know, in zsh, history meaning fc -l.

Then read man zshbuiltins, section about fc command:

Select a range of commands from first to last from the history list. The arguments first and last may be specified as a number or as a string. A negative number is used as an offset to the current history event number. A string specifies the most recent event beginning with the given string. All substitutions old=new, if any, are then performed on the commands.

...

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.

As the doc said, if negative number is used, it's an offset to the current history. So history -20 list command from current to 20 command before.

If you provide a number history 20, zsh thinks it's first to last form. In this case, first is set to 20, but last is omitted. So last is set to -1 because fc -l is used.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.