Search history for commands with grep

Much like we can return the history of commands executed in the terminal, we can also search specific commands executed by piping the history to grep pattern match.

In the example below, I want to return all g++ commands in my history.

$ history 1 | grep g++
   1001  g++ main.cpp -o hello
   1002  g++ main.cpp -o hello
   1006  g++ main.cpp -o hello
   1007  g++ main.cpp -o hello
   1009  g++ main.cpp -o hello
   1039  g++ -version
   1040  g++ --version
   1043  history 1 | grep g++
$