Alternative way to start reverse i-search without pressing ctrl + r in bash

The bash reverse i-search object is useful, but it is not like most other bash commands, since it seems to be associated with key bindings ( Ctrl + R ). How can a user run this tool using an alias or an input command?

+3
bash search history keyboard-shortcuts
Feb 24 '09 at 17:37
source share
2 answers

The reverse i-search function is actually a readline (reverse-search-history) function, not a bash built-in function (man builtin or see the built-in commands in the bash reference manual ). As far as I know, there is no way to call the readline function outside of the key binding.

You can see all readline key bindings to this function using " bind -l -p ". A list of related readline commands can also be found in the reference guide.

You can always use the history command to get a history list and use grep to find what you are looking for. I find this useful:

 alias hists="history | grep -v '^ *[0-9]* *hists' | grep $@" 

When I run hists something , I get a list of all the commands that match something . All I have to do is do it! # 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 

This is not exactly what you are looking for, but as close as I can do it.

+4
Feb 24 '09 at 20:59
source share

If you look at the manual pages for bash or history (3readline) under "Extending History"> "Event Labeling", you will see commands to execute this type.

 !?prof? 

will display the most recent command containing "prof" and execute it immediately. If you want to print the command without executing it, you can add ": p"

 !?prof?:p 

To edit a command on the command line using the example above, enter:

 !?prof 

(do not press enter) and press M - ^

They use the bash extension tool. They receive only the most recent match.

If you want to write scripts or aliases, look at the bash man page in the built-in fc and history commands (-p or -s). Also "shopt -s histreedit".

Here is an example of an alias:

 alias dothis='`history -p "!?jpg?"`' 

(those that are quoted only inside single quotes).

With some of these commands you can do cool "s / old / new" and other things.

+4
Apr 11 '09 at 3:34
source share



All Articles