0

Seems like history [number] is backward in zshell because it's using fc. How do I get history [number] to behave like it does in bash?

➜  exec bash
bash-3.2$ history 3
   35  history 5
   36  exit
   37  history 3
bash-3.2$ 

➜ history 3
    1  pwd
    2  ..
    3  mv work Documents

➜ history
   3133  history 5
   3134  exec bash
   3135  history
   3136  history 3
   3137  history -3
   3138  history 20

So in bash it behaves like I'd expect: last 3 commands. But in zshell it's backwards: the first 3 history commands. This is annoying and I'd like to fix it.

Closest I've got so far:

function history {
  fc -l -$1
}

But that still produces history starting at 1 instead of the last entry.

Related: Show older or newer history items with the history command in zsh

  • I don't use zsh, but.. alias history='history | tail -n${1-10}'? – DopeGhoti Nov 8 '17 at 21:46
2

It wasn't as difficult as I thought it would be. Added this to my ~/.profile

# zshell aliases history to "fc -l"

# quick version
alias h="fc -l -50"     # last 50

# history searching
function hg {
  echo "Searching for '$1'..."
  fc -l 0 | grep "$1"
}

Related and helpful: Show older or newer history items with the history command in zsh

| improve this answer | |
0

Looks like bash has it backwards, so if you flip the sign on the number...

$ PS1='%% ' zsh -f
% echo blah
blah
% echo de
de
% echo blah
blah
% history() { num=$(( -1 * $1 )); builtin history $num }
% history 3
    2  echo de
    3  echo blah
    4  history() { num=$(( -1 * $1 )); builtin history $num }
% 
| improve this answer | |
  • Uhh: history 3 | wc -l --> 3176; that's what I get when I run your first 2 lines. – jcollum Nov 8 '17 at 22:07
  • Probably oh-my-zsh bringing the usual derp. What happens under zsh -f with some history added? – thrig Nov 8 '17 at 22:28

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.