Bash re-launch history: a possible command to avoid using! bang !?

Scenario:

You make your daily bash shell material. You want to run the previous command to enter:

history | grep foocommand 

You will then get a list of all the foocommand tags that you have been doing for a long time when your history has been tracked in the list:

  585 foocommand --baz --bleet 750 foocommand | grep quux 987 history grep | foocommand 

You decide you want to run command 585, so type

  !585 

Question: Just for curiosity, is there a way to take this last step from the equation and still get the expected result? It would be nice if you could say:

  "grep through history and automatically run the first item on the list" 

or

 "grep through history and let me choose which item to run using the arrow keys" 
+4
source share
5 answers

Press ^r ( CTLR-r ), type foocommand press Enter;) This will search the history and display the last matching entry.

+5
source

Have you tried "! F"? 'f' as the first letter of foocommand.

+3
source

Syntax

 !foo 

the last command will start, starting with foo .

Otherwise, bash uses the readline library for input, which supports the history-search-forward and history-search-backward , which can be attached to the keys you select. I edited the ./inputrc file to associate them with F8 and Shift-F8, so that they work like the equivalent Windows function in the console when I connect to PuTTY:

 "\e[19~":history-search-backward "\e[32~":history-search-forward 
+1
source

Personally, I use an alias derived from ksh:

 alias r="fc -e -" 

('r' for 'reerun', I suppose ...)

I can restart the last command starting with 'foo':

 r foo 

I can reprogram commands 46 to 50:

 r 46 50 

I can re-execute command 585:

 r 585 

The only thing you got is if you typed:

 $ cd somewhere 

then running 'r cd' will not work, because there was extra space in front of the 'cd' command. The fc command is at the heart of the story, and you can edit commands 46 through 50 before restarting them by typing:

 fc 46 50 

etc. for other options.

("-e -" in the alias means "edit with a null editor", you can write "fc -e vim" for editing with vim, but most people install VISUAL, EDITOR or FCEDIT or all three to do what is not needed .)

Otherwise, being the person "vim" (fanatic?), I use "set -o vim" and then the search tool: ESC and

 /grep\ -e\ 'whatever 

to search history for a command containing "grep -e" regardless. I can repeat the search, moving further back in history or in the opposite direction after overshoot or ... I assume that emacs mode has an equivalent search mechanism.

+1
source

"grep through history and automatically start the first item in the list"

Good.

 `history | grep foocommand | head -1 | tr -s ' ' | cut -d ' ' -f 3-` 

Try it, he works like a champion!

0
source

All Articles