Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently switched from bash to zsh. In bash, one way (besides recursive search) that I used to find previously-run commands was history | grep whatever, where whatever is the bit of command I remember.

In zsh, this isn't working. history returns only a few items, even though my .zsh_history file contains many entries, which I have configured it to do.

How can I output my whole history, suitable for searching with grep?

share|improve this question
up vote 17 down vote accepted

History accepts a range in zsh entries as [first] [last] arguments, so to get them all run history 0.

To get the zsh help (at least with mind) type Alt-h over the history command and this will bring up the help for built-ins.

share|improve this answer
2  
Great! That's an annoying default for me, though, so I'm adding this to my aliases file: alias history="history 0" – Nathan Long Jan 12 '11 at 22:06
4  
Not that it matters but the history list starts at 1 and not 0. – ggustafsson Dec 2 '11 at 14:40
    
I'm using this alias myself: alias h='history 1 | grep' – Harald Nordgren Apr 2 at 15:03

The accepted answer is correct, but it’s worth noting that you don’t need to call the external grep binary to do the search, since that ability is baked in. I have this function defined in my .zshrc:

histsearch() { fc -lim "$@" 1 }

Notes:

  • fc is the zsh builtin that controls the interactive history. history is equivalent to fc -l.

  • The -m flag requires a pattern, which must be quoted.

  • The -i flag adds a timestamp.

  • fc has many more tricks up its sleeve (e.g. limiting the search to internal history for the current session). See the zshbuiltins(1) man page or the official documentation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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