The reverse-i-search facility in bash is useful, but it is unlike most other bash commands in that it seems to be bound to a keybinding (Ctrl-R). How can a user trigger this facility using an alias or typed-in command instead?
views:
938answers:
2The reverse-i-search function is actually a readline function (reverse-search-history) and not a bash builtin function (man builtin or see builtin commands in the bash reference manual). To my knowledge there is no way to call a readline function outside of binding it to a key.
You can see all the readline key binding to these fucntion using "bind -l -p
". The list of bindable readline commands can be found in the reference manual as well.
You can always use the history
command to get a list of the history and use grep
to find what you are looking for. Myself I find this to be useful:
alias hists="history | grep -v '^ *[0-9]* *hists' | grep $@"
When I run hists something
I get a list of all the commands that matches something
. All I have to do is then do !# to run the command. For example:
bash-3.2$ hists emacs
30 emacs
128 emacs
129 emacs
204 emacs
310 emacs .bash_history
324 emacs Documents/todo.txt
bash-3.2$ !324
It's not exactly what you are looking for but it's as close as I can make it.
If you look at the man pages for bash or history (3readline) under "History Expansion > Event Designators" you see commands for doing this type of thing.
!?prof?
would bring up the most recent command that contained "prof" and execute it immediately. If you want to print the command without executing it, you can append ":p"
!?prof?:p
In order to edit the command at the command line, using the example above, type:
!?prof
(don't press enter) and press M-^
These use the expansion facility of bash. They only get the most recent match.
If you'd like to write scripts or aliases, take a look in the bash man page at the builtin commands "fc" and "history" (-p or -s). Also "shopt -s histreedit".
Here's an example alias:
alias dothis='`history -p "!?jpg?"`'
(those are backquotes just inside the single quotes).
You can do some cool "s/old/new" and other stuff with some of these commands, too.