Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I have searched, but not found anything on this. I am looking for a functionality in bash, using a terminal.

Way back when, I had a user on a debian system, and a friend set me up with a convenient history search feature (I believe I used tcsh then), where I would type the beginning of a previous command, hit up-arrow, and it would do a search, based on the partial string.

E.g. if my history is:

./ arg1
cat output
cat output | grep yada

And I type ., and press up-arrow, it would show me: ./ arg1. Press it again and it would show ./, etc.

Very much like it would perform a grep on .bash_history. Is there a way to get this functionality?

share|improve this question
possible duplicate of iPython Like Command History For Shell – Marcel Stimberg Sep 3 '11 at 13:58
@Marcel Looks like it might be the same answer, I saw that in the search, but didn't connect it to my question, with the iPython and Shell keywords. – TLP Sep 3 '11 at 14:53

4 Answers 4

up vote 78 down vote accepted

Put the following lines in your ~/.inputrc:

## arrow up
## arrow down

Lines starting with # are comments. I can't remember what is backward and what forward. Experiment with it. Maybe you have to switch backward and forward.

A bit background information:

Bash is using readline to handle the prompt. ~/.inputrc is the configuration file for readline. Note that this will also take effect in other software using the readline library, for example IPython.

Read the bash manual for more information about readline. There you can also find more history related readline commands.

To get the escape codes for the arrow keys you can do the following:

  1. Start cat in a terminal (just cat, no further arguments).
  2. Type keys on keyboard, you will get things like ^[[A for up arrow and ^[[B for down arrow.
  3. Replace ^[ with \e.
share|improve this answer
+1: you beat me on time, good answer! – enzotib Sep 3 '11 at 13:34
That's exactly what I was looking for. Thanks! – TLP Sep 3 '11 at 14:51
Thank you so much, what a great tip – Sam Murray-Sutton Mar 12 '12 at 14:08
Thank You! You can't imagine how long I wanted this feature back from them times of 4DOS/4NT :) – Floyd May 1 '13 at 8:23
Thanks. This should be the default. – friederbluemle May 14 at 18:56

Ctrl+R will give you this sort of functionality. Start typing a partial command and it will come up with old ones which you can navigate with the up and down arrows. More info:

share|improve this answer
Thanks for your reply. Yes, I have tried that, but I find it a bit... finicky. It will not allow me to scroll through commands beginning with the partial, it only shows the first. It also searches the entire command for the character, which is sort of useless. – TLP Sep 3 '11 at 12:23
@TLP, repeat hitting Ctrl+r – akostadinov Oct 3 '14 at 21:54

Create a file named with mode 644 in /etc/profile.d/ with following content, login and check you preferred keys:

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

I think is the best way to do this. Mostly if you using configuration management systems such as chef, puppet, etc

And system config still untouched!

share|improve this answer
Oddly the ~/inputrc didn't work for me - but putting these commands into .bash_profile did. – Joe Jun 7 at 18:45

To complete lesmana's answer regarding "~/.inputrc" that I should create under my Trusty 12.04, verify/add in "~/.bashrc" (the first line already exists in my "~/.bashrc"):

shopt -s histappend  
PROMPT_COMMAND='history -a'  

It's already explained in French in

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.