Bash: select the previous command that matches the pattern

I know about navigating bash history with the Up and Down arrows.

I need a lazy way to select the previous command that matches some regular expression (this is shorter than the whole command, so it takes less time to enter).

Is this possible with bash?

If not, do other shells have such a function?

+3
source share
3 answers

You can always use back CTRL-Rto search your story and enter some part of the previous command. Pressing CTRL-Ragain (after the first hit) repeats your request (proceeds to the next match, if any).

( (AFAIK)):

# search (using perl regexp syntax) your entire history
function histgrep()
{
     grep -P $@ ~/.bash_history
}   

Edit: . this ( $PROMPT_COMMAND).

+3

Zsolt, , HISTFILE, : ${HISTFILE: - ~/.bash_history}; -)

, grepping ?! , (,! 33 33- ) grep-.

, , $@ () . , , "-" (histgrep -h), . , , "-" options, , , .; -)

, histgrep - ? h, i, Tab, g, Tab:)

^ R, | grep... . , , () :

function hgrep() { history | grep -P -- "$*"; }
+4

:

$echo happy
happy
$!?pp?
happy
+2

Source: https://habr.com/ru/post/1705079/


All Articles