Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On the bash command-line, gives me the previous command. On the command-lines in numpy or matlab, when I type a few characters, gives me the previously entered command starting with those characters. How can I enable exactly this behaviour in bash?

I am aware of more advanced ways of searching through the command-line history, but sometimes a simple way is more convenient.

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

The readline commands that you are looking for are the history-search-* commands:

history-search-forward
Search forward through the history for the string of characters between the start of the current line and the current cursor position (the point). This is a non-incremental search.
history-search-backward
Search backward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search.

Binding these in your .inputrc, like so:

"\e[A": history-search-backward            # arrow up
"\e[B": history-search-forward             # arrow down

will allow you to enter the first characters of a command, and then use the Up and Down keys to move through only those commands in your .bash_history that begin with that string.

For example, entering vi and the Up would take you to the first previous command beginning with vi, like vim somefile. Entering Up would take you to the next previous instance, and so on.

You can read more about all of the readline bindings here: http://linux.about.com/library/cmd/blcmdl3_readline.htm

share|improve this answer
add comment

Add the following to your .inputrc in ~/, to do what you suggested:

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

Perhaps you might also want to add the following to your .bash_profile :

export INPUTRC=~/.inputrc
share|improve this answer
add comment

Prepend a "!" to the command you are looking for. If you wish to replace the functionality of the up and down arrows, modifying the inputrc file (like the other answers suggest) is the way to go.

If "command" is the command you are searching for, enter this:

!comm

You should get "command" as the output.

Likewise, if you want to find a command that includes (but might not start with) part of the command, press ctrl+r and then type in part of the command.

You can also type in "tab" to auto complete your entered text (this includes files too). This is useful if you're looking for a command not in your history (or you don't have a history file)

You can read more about "mastering" the history file here: http://www.thegeekstuff.com/2008/08/15-examples-to-master-linux-command-line-history/

This is a possible duplicate of http://stackoverflow.com/questions/1030182/how-do-i-change-bash-history-completion-to-complete-whats-already-on-the-line

share|improve this answer
    
The other answers answer more correctly my question, but thanks for the pointer to SO (see also meta.stackexchange.com/q/4708/172266 ). –  gerrit Nov 4 '12 at 21:53
add comment

Your Answer

 
discard

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.