Grepping the ‘history’ command
I was updating my previous blog post when I remembered that I wanted to include a command that I ran but couldn’t remember. So I looked up how to grep the history command and thought I’d write make a small post about it to help solidify the ability to ‘pipe’ commands.
‘Grepping’ history
To grep the history command all you have to do is:
history | grep something_to_grep
Examples
Here are two examples of using grep with history:
history | grep test
history | grep git
Managing your history list
Thinking about this, there seems to be a lot that gets ‘remembered’ in your history. It would be cool if you could filter what gets saved to history in order to make finding past commands easier, particularly when using git since the same commands are used so frequently.
I am using zsh so I am going to list some of the environment variables supported by this shell for manipulating the history. This is the manual link that I referenced.
History-related files and environment variables
- By default, the history is saved to the
~/.zsh_history, this is stored in theHISTFILEand can be changed if desired - By default, the
$HISTSIZEwill be 2000 entries long, but if you useOh My Zshlike I do, then it will be increased to 50,000. I tried finding where this is done since it appears to overwrite the defaultHISTSIZEthatzshdefines, and this line of code in the public Oh My Zsh repo is the best I could come up with. It also looks like there may be a bug with regard to the relationship between the definitions of this environment variable zshusesHISTORY_IGNOREinstead of theHISTIGNOREvariable thatbashuses
Additional references
I was perusing different documentation and posts and saw all sorts of ways that the history can be manipulated, so rather then try to give recommendations (for now), I’ll link to some references I read when learning about how the history file can be manipulated:
zshdocumentation about History options- Moving to
zshPart 3 - Shell Options - StackExchange asking about unlimited history in
zsh - StackExchange inquiring about ignoring history duplicates
- StackExchange discussing options for customizing
zshon macOS - StackOverflow discussing viewing default
zshsettings - StackOverflow discussing
HISTIGNOREnot working inzsh - Someone else who also get engrossed in writing a blog about
zshhistory - A long blog about customizing your terminal
- A blog that begs the question if
Oh My Zshshould be installed at all
Conclusion
There is clearly a lot to learn about shell history and the history command. I hope to learn more about customizing zsh history, and when I do, if there is something that I feel should be added to this blog, then I’ll do it. Otherwise, hopefully these references help others on their learning paths. :)