10

In some terminals you can do something like this...

Type some command

nmap -sn 192.168.1.1/24

Then go on to do other things in the terminal for a while. Then later you can type

nmap

and then just press the key and it will index through all of the commands that started with what you typed, in this case nmap for example.

My example was pretty short but sometimes you type a very long command that you want to run again, and I know you can press until you find it again, but that goes through every command and could take a while if you typed it long ago.

Is there anyway to get this to work in Ubuntu's Terminal?

muru
  • 197,895
  • 55
  • 485
  • 740
Ryan Stull
  • 417
  • 2
  • 7
  • 20
  • 2
    Are you aware of !? Just in case: you can always type !nmap and this will execute the first command in the history that matches nmap. If you remember enough part of the command it can be faster than manually searching with arrows. Moreover you can modify the command using substitutions !nmap:s/something/something-else/. Although this doesn't request confirmation for execution of the command, so you'll see what is going to happen only when it's already started. – Bakuriu Apr 16 '15 at 11:54
  • 2
    Use ctrl-r and type nmap – Panther Apr 17 '15 at 03:09
  • Just to mention, I switched from bash to zsh which supports this out of the box – Ryan Stull Sep 23 '17 at 19:22

4 Answers4

26

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.

A.B.
  • 90,397
Robobenklein
  • 1,486
  • 16
  • 28
5

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.

JoKeR
  • 6,972
  • 9
  • 43
  • 65
5

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.

3

If you are in vi mode in bash (set -o vi), you can go in command mode (press Esc), and then type /whatever_you_want, followed by Enter.

It will search whatever you want, and you can scroll with j and k keys. Hit enter when you found the right command.

To be able to type commands when you have hitted Esc, you have to hit i (this is vi).

Going back to "normal" shell mode is done via set -o emacs.

muru
  • 197,895
  • 55
  • 485
  • 740