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?

3
  • 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, 2015 at 11:54
  • 2
    Use ctrl-r and type nmap
    – Panther
    Apr 17, 2015 at 3:09
  • Just to mention, I switched from bash to zsh which supports this out of the box
    – Ryan Stull
    Sep 23, 2017 at 19:22

4 Answers 4

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.

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.

2
  • 1
    That's pretty useful, not exactly what I was looking, but thanks for sharing!
    – Ryan Stull
    Apr 16, 2015 at 0:25
  • you're welcome, good luck :)
    – JoKeR
    Apr 16, 2015 at 0:30
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.

6
  • This is not the required answer, every linux user knows how to use vi editor, the question has nothing to do with do vi Apr 16, 2015 at 8:34
  • @Faizan bash, and many other shells, have vi keybindings, where you can press Esc and then vi-like keys to do things.
    – muru
    Apr 16, 2015 at 8:36
  • @muru i know it, but how does it answer the question? Apr 16, 2015 at 8:41
  • @Faizan Searching using / - an alternate to Ctrl-R given in the accepted answer.
    – muru
    Apr 16, 2015 at 8:42
  • / followed by nmap in current scenario . Apr 16, 2015 at 8:43

Not the answer you're looking for? Browse other questions tagged .