View Full Version : selection from terminal bash history
01-04-2010, 09:24 AM
If someone has the time, how can I use the command history to put an old command where the cursor is?
For example, I run history at the prompt and see the command I want at # 376. How do I get it to the prompt?
Will execute # 376 in the history list. If you want to just get it to the command line without executing, I don't know. Copy and paste?
01-04-2010, 10:15 AM
Just type an exclam (!) and then the number you want:
That will execute it.
To avoid it being executed, add ":p" at the end:
Then you can up-arrow and edit it.
But you really should read about shell history mechanisms in a Bash tutorial (see e.g. the last section of this Unix FAQ (http://hayne.net/MacDev/Notes/unixFAQ.html)) or just via 'man bash' (scroll to the HISTORY section).
01-04-2010, 10:35 AM
Man, you guys are great thanks a bunch. More info than I expected and certainly more useful, thanks.
ok, ended up at http://hayne.net/MacDev/Notes/unixFAQ.html again. Hmmm, how many times can I read that thing over. Apparently I have zero tolerance for unix knowledge absorption.
01-04-2010, 10:42 AM
And be careful when invoking previous commands this way. Depending on your configuration, invoking a previous command may reshuffle the history such that the same number now refers to a different command.
01-04-2010, 10:58 AM
reshuffle the history such that the same number now refers to a different command.
O yes I see, I will look into that and figure out a solution. Thanks
01-04-2010, 10:49 PM
Good answers everyone! As a side note to the discussion...
If this is a long command with arguments that you will be executing often, you may want to consider setting up an alias for the command in your bash profile.
For example, I frequently ssh into my Mac mini that I use as a home entertainment center, hooked up to my HDTV. With my alias, all I have to do is type 'mini' at the command line, and I've got an ssh session up and running quickly.
01-04-2010, 11:58 PM
If some of the text in the old command is known (which is highly likely), type ctrl-R to search backwards (by entering said text afterwards). Once enough chars are entered, subsequent ctrl-R calls will jump back in time to previous matches. Use ctrl-J (or any <- arrow -> key) to stop the search, thereby selecting the current result on the command line... or type ctrl-G to abort the search entirely, returning to the starting point (i.e., empty command line at the end of history).
Once that all becomes 2nd nature (it's much easier to do than it is to explain), you'll seldom use history numbers and/or tricky event syntax again (other than sudo !! perhaps).
Try this url in your browser for more info: file:///usr/share/doc/bash/bashref.html#SEC98
vBulletin® v3.8.7, Copyright ©2000-2014, vBulletin Solutions, Inc.