Get previous commands in Terminal that match the currently typed command [duplicate]

Solution 1:

Yes, there is a very simple way to search through your command history. When at the terminal, press Ctrl-R to begin a search, then you can type nmap and it will search back to the last command using nmap.

If you don't want the last command that contained the 'nmap' word but some other further to the past, then you can hit Ctrl-R again as many times as you'd like.

Solution 2:

I always use history it's just more convinient for me to see all the commands that I type for example:

history

lists you all commands that you typed for a while

history | grep nmap

lists you only commands with nmap

history | grep nmap | tail -10

history | grep nmap | head -10

lists you last and first 10 commands with nmap

then just copy and paste it again.

Solution 3:

The way that I prefer to use to achieve this is by re-maping the Up and Down keys to Bash's history search. This can be achieved by adding the following to .inputrc:

"\e[A": history-search-backward
"\e[B": history-search-forward

After reloading your shell, pressing Up or Down on an empty prompt will navigate through all of the commands, and pressing Up or Down after typing e.g. nmap will navigate through all of the commands that started with what you typed.