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?

up vote 35 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.

  • 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
  • 6
    Not that it matters but the history list starts at 1 and not 0. – ggustafsson Dec 2 '11 at 14:40
  • 2
    I'm using this alias myself: alias h='history 1 | grep' – Harald Nordgren Apr 2 '16 at 15:03
  • 1
    This doesn't seem to work for me: my ~/.zsh_history has 10066 lines, but history 0 (or its equivalent, fc -l 0), only prints out 4999 lines. – jayhendren May 31 '17 at 17:29

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.

  • Better yet is to use "*$@*" (note additional stars) pattern instead of "$@" as the latter yields only exact matches. – Piotr Dobrogost May 7 at 11:50
  • You’re completely right, @PiotrDobrogost! I’m not sure how I managed to submit this answer in the state that I did — I’m guessing I must’ve typed that function from memory. As it stands it’s not very usable, so I’m going to edit the answer to incorporate the asterisks. – wjv May 7 at 13:15

Have a look at fzf. It helps not only finding "whatever-particles" in your shell history, but also in other interesting places, e.g. browser history, directory history, etc.

fzf is a command-line fuzzy finder. That means you can search for particles or fractions of what you are looking for and it will display a collection of matches which you can continuously refine. It's really a game changer.

The homepage of the author contains a number of illustrative examples.

  • Consider adding some reference to this answer supporting what you state. – Pimp Juice IT Oct 9 '17 at 22:44
  • Welcome to Super User! Please read How to recommend software for minimum required information and suggestions on how to recommend software on Super User. To keep your answer useful even if the provided link(s) breaks these details should be edited into your answer. – Twisty Impersonator Oct 9 '17 at 23:31

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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