Terminal command history search

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
all41
Level 19
Level 19
Posts: 9520
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Terminal command history search

Post by all41 »

When using the terminal the up-arrow will scroll through previously entered commands stored in .bash_history.
That's an excellent way to find a recently used command for inspection or reuse, but to find a command
used a few weeks ago means tediously repeating up-arrow to locate it.

You can add a simple file that will allow an alphabetical search of previously used commands with their syntax and arguments.

example:
A couple months ago I used the terminal and entered:
xrander --listactivemonitors
Yesterday I connected a different monitor and wanted to repeat that command but
didn't remember the exact syntax of the command arguments.
With the terminal open I typed x, then arrow up--found it on the first tap! The xrander -listactivemonitors command was
entered and ready to use---so two keystrokes found a months old command.

If you can remember even the first letter of the command the up-arrow will find it more quickly.
Entering even more letters of the command narrows and speeds the search results even more.

example:
if you have previously used the cat command just type cat, then up-arrow will scroll through
the cat commands previously used.

This means less typing for lengthy commands--along with less fat-fingerered/pebkac/memory lapse errors.

And---it's easy

Just make a document named .inputrc and copy/paste the following:

Code: Select all

$include /etc/inputrc

# command history search
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char

# extended auto-completion with tab
set show-all-if-ambiguous on
set completion-ignore-case on


# colors
set colored-completion-prefix on
set colored-stats on

# misc
set blink-matching-paren on
set mark-symlinked-directories = on
Put this document in your home directory so the path will be:
/home/username/.inputrc

Note that this document begins with a decimal point and will
become a hidden file. ctrl+h to show
Everything in life was difficult before it became easy.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Terminal command history search

Post by Termy »

Simply typing in `cat` then browsing up in the history (Up or Ctrl + P) does not do what you think it does; for that, you can use Ctrl + R, and repeat as needed, for reverse history searching, which is amazingly useful. :) Unless you have some non-standard configuration unknown to me.

I believe this line of interest to ensure you have, is:

Code: Select all

"\e[A": history-search-backward
Which, IIRC, means to bind the Ctrl + R shortcut to that functionality.
I'm also Terminalforlife on GitHub.
User avatar
paddys-hill
Level 1
Level 1
Posts: 33
Joined: Mon Jun 14, 2021 5:07 pm
Location: All over the Adelaide Hills and Fleurio region of South Australia

Re: Terminal command history search

Post by paddys-hill »

A reasonable bash key cheatsheet is https://ss64.com/bash/syntax-keyboard.html
Paddys-Hill Co-op co-ordinator & sysadmin
Acer Aspire A315-21G (AMD A9-9420 RADEON R5) running 5.11.0-22-generic
Distro: Linux Mint 20.1 Ulyssa DE:Cinnamon 4.8.6 WM: muffin 4.8.1 DM: LightDM 1.30.0
Moonstone Man
Level 16
Level 16
Posts: 6054
Joined: Mon Aug 27, 2012 10:17 pm

Re: Terminal command history search

Post by Moonstone Man »

all41 wrote: Mon Jun 21, 2021 11:20 am When using the terminal the up-arrow will scroll through previously entered commands stored in .bash_history.
alias hg='history|grep' works fine for me.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Terminal command history search

Post by rene »

Don't be so quick to say that; Ctrl-R and with or without editing of the found instance is extremely convenient.
Post Reply

Return to “Tutorials”