Terminal command history search

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Post Reply
User avatar
Level 17
Level 17
Posts: 7032
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.

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.

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:

Note that this document begins with a decimal point and will
become a hidden file. ctrl+h to show
User avatar
Level 7
Level 7
Posts: 1781
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK

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.
Confused? Try this guide to the Linux Mint Support Forums!

I'm Terminalforlife (LL) on YouTube and terminalforlife on GitHub.
User avatar
Level 1
Level 1
Posts: 34
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
User avatar
Kadaitcha Man
Level 15
Level 15
Posts: 5636
Joined: Mon Aug 27, 2012 10:17 pm

Re: Terminal command history search

Post by Kadaitcha 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.
[*] All kadaitcha men are liars
Level 17
Level 17
Posts: 7748
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