Pimp your shell history

A seemingly little known input option can transform the way you use your shell:

cat >> ~/.inputrc <<'EOF'
# narrow history to input text left of the cursor
$if bash
	"\e[A": history-search-backward
	"\e[B": history-search-forward
$endif
EOF

That is the single most valuable option you’ll ever need; it’s essentially how the command history works OOTB in vim. It sets and to search the history for lines starting with what’s to the left of the cursor.

Take this current real life example:

How it transformed my shell usage

Using that inputrc option, I don’t need to write many scripts, because I can just retrieve my ad hoc scriptlets with a simple search pattern; I have a lot of those in my history. To be perfectly honest, though, I kinda sorta lied, when implying that I’m using Bash, because ever since I had my first experience with GRML’s non-plus ultra excellent Zsh setup, I have switched to Zsh and never looked back; it too did need a little tweak of the config, though:

bindkey "^[OA" history-beginning-search-backward
bindkey "^[OB" history-beginning-search-forward
bindkey "^[[A" history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward

because it has its own line editor zle; that way the cursor stays in position, just like with the ~/.inputrc setting, so one can refine the search, if there are too many matches. Keeping with the ip link example, suppose you have run other commands starting with sudo ip between the last MTU adjustments and typing sudo ip gets you sudo ip address ...; with the cursor still just after ip, you can refine the search by typing link and so on — you get the gist.

That, combined with the power of Tab (menu) completion everywhere has made me into a desktop refugee who practically lives in a shell. I always cry a little inside whenever I am forced to use any default shell setup, which doesn’t have these rather basic but very very powerful options set, so virtually all of them — except GRML, of course, which has the slightly inferior default setting that puts the cursor at the end of the line but showed me that this was even possible. I am truly baffled that this isn’t the default. Maybe someone just needs to spread the word; happy to oblige:
:trumpet: :stadium: :tada: :confetti_ball: :smiling_face_with_sunglasses:
@Canonical

1 Like

CtrlR and CtrlS open a backward or forward history search in bash (and afaik in zsh too, other shells probably provide similar commands).

To scroll through history entries within search just hit CtrlR and CtrlS again. The search string may match anywhere in a history line.

Referring to the example by @peterwhite23 hitting CtrlR, entering 1320 will most likely bring up the sudo ip ... mtu 1320 command (else just search backward or forward).

More at bash reference and in man bash. Excerpt from man page:

       reverse-search-history (C-r)
              Search backward starting at the current line  and  moving
              “up” through the history as necessary.  This is an incre‐
              mental  search.   This  command  sets  the  region to the
              matched text and activates the region.
       forward-search-history (C-s)
              Search forward starting at the current  line  and  moving
              “down”  through the history as necessary.  This is an in‐
              cremental search.  This command sets the  region  to  the
              matched text and activates the region.

Ubuntu 26.04 with bash

echo $0
bash

First effort reported syntax error
.inputrc not created

$ cat >>| ~/.inputrc <<'EOF'
# narrow history to input text left of the cursor
$if bash
        "\e[A": history-search-backward
        "\e[B": history-search-forward
$endif
EOF
bash: syntax error near unexpected token `|'

I removed the pipe
.inputrc successfully created without syntax error

$ cat >> ~/.inputrc <<'EOF'
# narrow history to input text left of the cursor
$if bash
        "\e[A": history-search-backward
        "\e[B": history-search-forward
$endif
EOF

2 Likes

Oopsy, a Z-Shellism sneaked in! :innocent:

Thanks! I’ve fixed the post.