DEV Community

Cover image for A Better History Command
Hai Vu
Hai Vu

Posted on

2

A Better History Command

Problem

Many people using the Linux terminal repeatedly search the command line history with such command:

history | grep cd
Enter fullscreen mode Exit fullscreen mode

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 "${@:-}"
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
dothtm profile image
dotHTM

I really appreciate the fish shell’s history command and completions.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay