Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History sub search in Bash #1506

Closed
emanuelhfarias opened this issue Feb 26, 2020 · 15 comments · Fixed by #1764
Closed

History sub search in Bash #1506

emanuelhfarias opened this issue Feb 26, 2020 · 15 comments · Fixed by #1764

Comments

@emanuelhfarias
Copy link

emanuelhfarias commented Feb 26, 2020

Does anybody know a history sub search plugin for Bash?

In Fish and Zsh there is a plugin called history-substring-search that you can type part of a sentence and press Up Arrow, it will complete the sentence according to history and highlight the matching words.

I miss it in Bash. I already use Ctrl+R but it not the same, I don't feel productive.

@nwinkler
Copy link
Member

I'm not familiar with these features in Fish and Zsh, but there has been a similar discussion here in #894.

There are some great suggestions in that ticket. Personally, I use fzf with the Bash-it fzf plugin, which overrides the default Ctrl+R behavior, that works really well for me.

Please let me know if that helps.

@emanuelhfarias
Copy link
Author

emanuelhfarias commented Feb 29, 2020

I've found that GNU Readline has a feature called history-substring-search-backward which is what I'm looking for: you can type any part of a previously entered command and press Up Arrow, it will complete it for you.

Since Bash 4+ this feature is available.
You just need to configure it in ~/.inputrc file:

"\e[A": history-substring-search-backward
"\e[B": history-substring-search-forward

Then, Ctrl+x Ctrl+r to reload configs from ~/.inputrc, or just reload bash.

This is faster than using Ctrl + r 😄

The only thing is missing is the highlighting what has been matched. But I can live without that.

Thanks

@jpmcb
Copy link
Contributor

jpmcb commented Mar 1, 2020

Interesting - is there the concept of a configurable inputrc for bash-it? The whole mantra of this project seems to be "reduce the number of cluttered *rc files".

Would it be possible to include this as a plugin / feature so someone could use bash-it instead of manually adding a line to their inputrc

@emanuelhfarias
Copy link
Author

@jpmcb It's also possible to active it in ~/.bashrc like this:

bind '"\e[A": history-substring-search-backward'
bind '"\e[B": history-substring-search-forward'

So we don't need the ~/.inputrc anymore.

It's possible to create a plugin to activate this.

@nwinkler
Copy link
Member

nwinkler commented Mar 2, 2020

Exactly - these two lines would need to be defined in a plugin, e.g. bash-history or something like that.

@nwinkler
Copy link
Member

nwinkler commented Mar 2, 2020

OK, looks like we already have that:

https://github.com/Bash-it/bash-it/blob/master/plugins/available/history.plugin.bash

You can enable that with bash-it enable plugin history. It uses history-search-backward, without the substring part, but that could be adjusted, or configured through an environment variable if needed.

@mcarans
Copy link

mcarans commented Jan 4, 2021

@nwinkler Thanks so much for making bash-it. I had tried out fish shell before and was looking for something like this for bash.

I'm not clear what I need to change to make the history plugin use the substring part as you described above. Please could you describe how to do it for a bash-it newbie!

@cornfeedhobo
Copy link
Member

@mcarans You just need to enable the plugin and reload your shell (or start a new one):

$ bash-it enable plugin history
$ bash-it reload

You may also enjoy our new documentation at https://bash-it.readthedocs.io/en/latest/

@mcarans
Copy link

mcarans commented Jan 4, 2021

@cornfeedhobo The problem is that the current history plugin uses history-search-backward not history-substring-search-backward (https://github.com/Bash-it/bash-it/blob/master/plugins/available/history.plugin.bash). Is there a way to modify the installed history plugin to use history-substring-search-backward?

@cornfeedhobo
Copy link
Member

@mcarans The original post asked:

history-substring-search that you can type part of a sentence and press Up Arrow, it will complete the sentence according to history and highlight the matching words.

That is exactly what the history plugin does. When I type ech and hit the up arrow, the only commands shown are ones that begin with the ech prefix, e.g. echo "foobar".

Are you looking for something different?

@mcarans
Copy link

mcarans commented Jan 4, 2021

@cornfeedhobo That works for me! Out of interest what would the addition of substring do if anything?

@mcarans
Copy link

mcarans commented Jan 4, 2021

@cornfeedhobo I found the answer here

history-search-backward: The search string must match at the beginning of a history line
history-substring-search-backward: The search string may match anywhere in a history line.

It seems to me like history-substring-search-backward is more useful. Is there any way I can change the installed plugin to use history-substring-search-backward?

@cornfeedhobo
Copy link
Member

@mcarans Sorry got pulled away. Looks like you figured it out - substring means that if you type in echo and hit the up arrow, it will show any command which contains the string "echo", regardless of position. Thanks for sticking with it!

I definitely think we can work something in to address this option, just need to think of the most elegant way to expose that option to users. I'm not sure if I'd prefer breaking up history into different plugins, or using an environment variable to toggle the functionality.

@tbhaxor @nwinkler @NoahGorny @davidpfarrell any thoughts?

@nwinkler
Copy link
Member

nwinkler commented Jan 5, 2021

@cornfeedhobo I'd go with an environment variable to change which command is being used. Adding a second plugin will lead to further complexity, e.g. when people enable both plugins. Using an environment variable, we can have a defined default behavior and also an easy option for overriding things. A couple of items to address:

  • We should aim for consistent naming patterns for plugin configuration options - don't think we have a consistent pattern at the moment.
  • How do we document plugin configuration settings? Do we want to support an optional documentation file per plugin?

@cornfeedhobo
Copy link
Member

cornfeedhobo commented Jan 5, 2021

Since I was waiting around and ended up coming to the opposite conclusion, I'm going to continue that discussion in #1764, at least until we decide if we want to go with that route or another one. Thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants