tags:

views:

435

answers:

10

When working an interactive bash session, one aspect from the Windows shell I miss is the F8 key where you start typing a command, hit F8 and the shell finds the most recent command entered in history that matches what you have typed so far. e.g.

me@Ubntu07:~>cd /home/jb<F8 Key Here>

brings up my prior command:

me@Ubntu07:~>cd /home/jboss/server/default/log

Is there any way to do this in bash ?

+10  A: 

Hit Ctrl-R before you start typing.

(There may well be another version which finds commands based on what's already been typed - I wouldn't know, as Ctrl-R has always been good enough for me :)

Pressing Ctrl-R again shows the next match etc.

Jon Skeet
Note that if you tape after your first Ctrl-R, you will restrict the search in the history to entries matching what you typed; you can then alternate between Ctrl-R and typing to find quickly what you are looking for.
MatthieuP
+1  A: 

In your case !jb would print and then run that command.

e.g.,

$ nano logconfig.properties
$ !n
nano logconfig.properties
$

Of course if you want to be on the safe side, use ctrl-r first to bring up the interactive command history.

JeeBee
His command starts "cd /home/jb"; "!jb" would not run his command. "!?jb?" might do it, but it also might run some other command that had the letters "jb" in it.
Rob Kennedy
If you get in doubt once you've already typed the ! and search part of the query, you can force in-place expansion with M-^.
JB
+1  A: 

CTRL+R does a history search. It's a bit different in that first you hit CTRL+R and then type what you're looking for.

sblundy
A: 

Ctrl + r and start typing.

I think. I don't have a bash shell here to try it out on.

Stever B
+1  A: 

If you're just talking about a command, you can use the !<cmd> to do the last one. For example, say you entered python runscript.py a while ago; you can type:

!py

or something along those lines to run that command again.

To repeat an argument to a command, you could do something like this:

echo !py:1

which would echo runscript.py back to the terminal, in this example. The number after the colon refers to the argument you'd like to use from the given command.

There's a lot of other great information about the bash history here.

mipadi
+4  A: 

My Gentoo is configured in a way that I can press PgUp and PgDn to scroll through those commands in the command history that start with what’s currently in my command line.

# cd<PgUp>

results in:

# cd hydrogen

That’s pretty much the same function. It is defined in my /etc/inputrc with the following lines:

# mappings for "page up" and "page down" to step to the beginning/end 
# of the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
Bombe
I have something similar in tcsh. I have it mapped to Ctrl+Up and Ctrl+Down instead, so I can have normal history-browsing as well.
Rob Kennedy
A: 

IMHO '!' is dangerous, like doing !rm in the wrong shell that has a different rm command from what you were expecting. At least CTRL-R lets you see the full command line you selected before executing it.

FredV
+1  A: 

I have these lines in my .inputrc file:

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

This binds history search to the up and down arrow keys. So you can start typing a command, kextload say, and then each tap of the up arrow will complete the line with the previous command that started with kextload.

All of my config files are public on github.

http://github.com/jonshea/config-files/tree/master

Jon Shea
A: 

Nice solutions. Thanks for those. The Ctrl-R was exactly what I was looking for, although the inputrc options are really useful add-ons.

Cheers.

Nicholas
A: 

If you use vi input mode (set -o vi in bash or via set editing-mode vi in .inputrc), you can use normal vi commands to search the history (/). This gives you full regular expressions, too, which can be helpful for finding a complex command.

Hudson