Shell Command History – How to Quickly Store and Access Often Used Commands

command historyshell

I have a lot of commands I routinely need to execute, often with the slightest variation.

Right now I'm storing them all in .bash_history and use CTRLR to access them, but I wonder if there's a better way. What I'm looking for:

  • Easy to add a new command
  • Easy to search and re-execute a wanted command
  • Avoid unwanted commands in suggestions

Unfortunately, bash_history is not so strong on the third demand: if I do a few cd and ls, it fills the history file quickly. I have recently learned about HIST_SIZE and that you can configure the history to avoid duplicates or certain commands, but before configuring all that, I wanted to make sure it is the best way.

Best Answer

I find very useful the following readline commands

history-search-backward,
history-search-forward

(be aware they are different from the usual reverse-search-history, forward-search-history, tied to Ctrl-R, Ctrl-S).

I have these commands associated to Ctrl-Up and Ctrl-Down putting the following lines into ~/.inputrc:

"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward

How they work: write few chars of the beginning of the command, press Ctrl-Up and the next older command starting with that prefix will be shown, press again to see the next, and so on. When you are satisfied, after possibly modifying the command, press Enter to execute.

Related Question