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
Top comments (1)
I really appreciate the fish shell’s history command and completions.