Hai Vu
Posted on October 10, 2023
Problem
Many people using the Linux terminal repeatedly search the command line history with such command:
history | grep cd
While this command is fine, I would like to shorten the command.
Design
I would like to create a command h
which will
- List the history if there is no argument
- Search the history for the arguments when they are supplied
Implementation
After a few trials and error, I finally came up with a shell function, which I add to my ~/.bashrc
and ~/.zshrc
files:
function h() {
history | grep -E --color=auto "${@:-}"
}
Usage
When invoked without any arguments, the command h
behaves just like the history
command.
When invoked with one or more arguments, those arguments will be passed on to the grep
command. Examples:
h # shorthand for history
h ls # Searches history for the ls command
h -i myfile # Case-insensitive search for myfile
💖 💪 🙅 🚩
Hai Vu
Posted on October 10, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
tutorial Configure Git with Multi-Account SSH and Verified Commits Using GPG in Github
November 28, 2024
ansible Ansible Demystified: Your Ultimate Guide to Simplifying IT Automation and Boosting DevOps Efficiency
November 20, 2024