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

How to prevent navigating in the command history (arrow keys) from requiring pressing the Return key twice #531

Open
NitramO-YT opened this issue Nov 24, 2024 · 13 comments
Labels
duplicate Duplicate / Already fixed question Question / Mis-usage

Comments

@NitramO-YT
Copy link

Hi, is there any way to prevent this behavior or disable the option which do that when you navigate through the command history (arrow keys) you then have to press enter twice to execute the command? (Something else than pressing Ctrl+J everytime instead of Return)

Thanks in advance for your help!

@akinomyoga
Copy link
Owner

With the default setting, ble.sh doesn't behave that way. This is most likely related to the behavior of the non-incremental history search mode triggered by keybindings of history search-{for,back}ward. Then, this should be a duplicate of #382.

@NitramO-YT Could you check the reply to #382?

@NitramO-YT
Copy link
Author

Okay, i checked my ~/.bashrc and when i disable oh-my-bash i don't have the problem anymore, but i also tested only oh-my-bash without ble.sh and it also works, i only get the issue when the both are enabled!

@akinomyoga
Copy link
Owner

akinomyoga commented Nov 25, 2024

Yes, but that's intentional. The goal of ble.sh is not to perfectly reproduce the Readline behavior but to improve the Readline behavior closer to real text editors. Here, the history search is, by default, intended to be an independent mode, which needs to be explicitly terminated by pressing RET or another key. Nevertheless, ble.sh still allows users to configure it.

@NitramO-YT Could you check the reply to #382 for the adjustment of the configuration? The adjustment necessary to achieve the behavior you describe is also described there. It is also described in the Q&A page (the fifth item).

@akinomyoga akinomyoga added the duplicate Duplicate / Already fixed label Nov 25, 2024
@NitramO-YT
Copy link
Author

Yes sorry, i was trying it after i wrote my last answer, so it works, but i would like to not have this part under of the line while navigating trough the history, is it possible?
image

Or do you think it's possible to replace it by fzf-history's one?
(Like in my ~/.bashrc i have this :

[ -f ~/.fzf.bash ] && source ~/.fzf.bash

bind '"\C-r": reverse-search-history'
bind -x '"\C-f": __fzf_history__'

To replace the fzf replacement of Ctrl+R by the classic Ctrl+R and bind fzf search to Ctrl+F)

@akinomyoga
Copy link
Owner

Yes sorry, i was trying it after i wrote my last answer, so it works, but i would like to not have this part under of the line while navigating trough the history, is it possible?

That is a duplicate of #80 and #101. As described in Maunal §4.11.1, you can specify hide-status to OPTS in addition to immediate-accept.

Or do you think it's possible to replace it by fzf-history's one? (Like in my ~/.bashrc i have this :

Yes.

[ -f ~/.fzf.bash ] && source ~/.fzf.bash

Please read README. It is described in README §2.8. Also, there is a pointer in the Note at the top of README.

bind '"\C-r": reverse-search-history'

The readline bindable function reverse-search-history is not the classic Ctrl+R as you intend. The default C-r is history-isearch-backward (i.e., the incremental backward history search).

If you really want to use reverse-search-history (i.e., the non-incremental history backward search), to specify immediate-accept:hide-status, please use ble-bind as described in #382.

bind -x '"\C-f": __fzf_history__'

Please use the following instead (Note: you need bash >= 4.0):

ble-bind -x C-f fzf-history-widget

@NitramO-YT
Copy link
Author

Okay, let's forget about fzf for now, i read #80 & #101 and i still don't understand how to do what i want, but by reading it i learned about Ctrl+P and Ctrl+N and i tried it and it's exactly what i want but for the arrow keys,

also with

ble-bind -f up 'history-search-backward immediate-accept'
ble-bind -f down 'history-search-forward immediate-accept'

if you press arrow up many times and then Ctrl+C to quit and then arrow up again and press arrow down to go back then you can go infinitely to after the last command
Enregistrement d'écran_20241125_182353.webm

@NitramO-YT
Copy link
Author

Okay, so ChatGPT explained me, i did

ble-bind -k 'UP'   'history-search-backward:immediate-accept:hide-status'
ble-bind -k 'DOWN' 'history-search-forward:immediate-accept:hide-status'

in my ~/.blerc and it works! Thanks for your help!

@NitramO-YT
Copy link
Author

Okay, so ChatGPT explained me, i did

ble-bind -k 'UP'   'history-search-backward:immediate-accept:hide-status'
ble-bind -k 'DOWN' 'history-search-forward:immediate-accept:hide-status'

in my ~/.blerc and it works! Thanks for your help!

Well this works, but actually places my Caret Cursor at the start of the line and not at the end like how it should do natively, is there any fix?

@akinomyoga
Copy link
Owner

akinomyoga commented Nov 25, 2024

Okay, let's forget about fzf for now, i read #80 & #101 and i still don't understand how to do what i want, but by reading it i learned about Ctrl+P and Ctrl+N and i tried it and it's exactly what i want but for the arrow keys,

So what do you want to achieve? I thought you wanted the setting in oh-my-bash but wanted to run a command with a single RET. In addition, I thought you wanted to hide the status. If that is the case, the setting would be:

ble-bind -f up 'history-search-backward immediate-accept:hide-status'
ble-bind -f down 'history-search-forward immediate-accept:hide-status'

However, if you wanted the behavior of C-p and C-n for up and down, it is actually the default setting. The behavior difference is because you or oh-my-bash overwrite the keybindings for up and down. You can just remove these settings overwriting the default one.

Or, while still loading oh-my-bash, you can again overwrite the keybindings to the default one in ~/.bashrc (which needs to be later than source "$OSH"/oh-my-bash.sh):

ble-bind -m 'emacs' -f 'up' 'backward-line history'
ble-bind -m 'emacs' -f 'down' 'forward-line history'
ble-bind -f up 'history-search-backward immediate-accept'
ble-bind -f down 'history-search-forward immediate-accept'

if you press arrow up many times and then Ctrl+C to quit and then arrow up again and press arrow down to go back then you can go infinitely to after the last command

What do you mean by "infinitely"? Doesn't that as many times as you pressed up instead of the infinite times? If it means really the infinite times, I cannot reproduce the behavior. If it means as many times as you have pressed up, it is another intentional change to the Readline behavior. The behavior is controlled by the action=ACTION option, which is also described in Manual §4.11.1. In Readline, those nsearch do not change the actual position in the command history while loading commands from the history. In contrast, ble.sh's nsearch implementation actually navigates through the history. If you want to use the Readline behavior of nsearch, you can specify action=load:

ble-bind -f up 'history-search-backward immediate-accept:hide-status:action=load'
ble-bind -f down 'history-search-forward immediate-accept:hide-status:action=load'

Okay, so ChatGPT explained me, i did

ble-bind -k 'UP'   'history-search-backward:immediate-accept:hide-status'
ble-bind -k 'DOWN' 'history-search-forward:immediate-accept:hide-status'

This is nonsense...everything is wrong. Fortunately, this just defines four key names (UP, DOWN, history-search-backward:immediate-accept:hide-status, history-search-forward:immediate-accept:hide-status) internally but does not affect any behavior because there are no keys with such names (it should be noted that key names are case sensitive in ble.sh, so UP and DOWN are distinct from up and down).

Well this works, but actually places my Caret Cursor at the start of the line and not at the end like how it should do natively, is there any fix?

Is that really what you have in your ~/.blerc? I suspect other parts of your settings causes the behavior you observe. If you see your cursor at the beginning of the line while navigation, you'd likely have the keybindings of nsearch to up and down somewhere in your settings. In that case, this behavior can also be configured as described in Manual §4.11.1. You can use point=POINT to adjust the behavior. Since the default is point=match-begin, if you start the search with an empty string, it matches the zero width range at the beginning of each history entry, and then the cursor would be placed on the first character. If you always want the cursor at the end of the line, you can specify point=end:

ble-bind -f up 'history-search-backward immediate-accept:hide-status:action=load:point=end'
ble-bind -f down 'history-search-forward immediate-accept:hide-status:action=load:point=end'

@akinomyoga
Copy link
Owner

akinomyoga commented Nov 25, 2024

So, to summarize, I don't know what you actually want because the description seems inconsistent. While you say you want the behavior of C-p and C-n, your request seems inconsistent with the behavior of C-p and C-n. Anyway, you might try the following settings and choose the one you like:

# option 1
ble-bind -f up 'history-search-backward immediate-accept:hide-status'
ble-bind -f down 'history-search-forward immediate-accept:hide-status'

# option 2
# (set no keybindings but remove oh-my-bash)

# option 3
ble-bind -m 'emacs' -f 'up' 'backward-line history'
ble-bind -m 'emacs' -f 'down' 'forward-line history'

# option 4
# (set no keybindings)

# option 5
ble-bind -f up 'history-search-backward immediate-accept:hide-status:action=load'
ble-bind -f down 'history-search-forward immediate-accept:hide-status:action=load'

# option 6
ble-bind -f up 'history-search-backward immediate-accept:hide-status:action=load:point=end'
ble-bind -f down 'history-search-forward immediate-accept:hide-status:action=load:point=end'

# option 7
ble-bind -f up 'history-search-backward point=end'
ble-bind -f down 'history-search-forward point=end'

# option 8
ble-bind -f up 'history-search-backward immediate-accept:hide-status:point=end'
ble-bind -f down 'history-search-forward immediate-accept:hide-status:point=end'

@NitramO-YT
Copy link
Author

This is nonsense...everything is wrong. Fortunately, this just defines four key names (UP, DOWN, history-search-backward:immediate-accept:hide-status, history-search-forward:immediate-accept:hide-status) internally but does not affect any behavior because there are no keys with such names (it should be noted that key names are case sensitive in ble.sh, so UP and DOWN are distinct from up and down).

Sorry, i pasted the wrong lines, this wasn't working so i asked again and he gave me

ble-bind -f up 'history-search-backward immediate-accept:hide-status'
ble-bind -f down 'history-search-forward immediate-accept:hide-status'

That was these lines which were working as i wanted (apart from the wrong cursor placement)

So i tried both

ble-bind -f up 'history-search-backward immediate-accept:hide-status:action=load:point=end'
ble-bind -f down 'history-search-forward immediate-accept:hide-status:action=load:point=end'

and

ble-bind -m 'emacs' -f 'up' 'backward-line history'
ble-bind -m 'emacs' -f 'down' 'forward-line history'

in my ~/.blerc and they seems to work exactly as how i wanted!

To explain again what i wanted was:
Navigating through the bash commands history with the arrows, without any visual info (like by default without ble.sh or oh-my-bash) and only need to press return once! So the lines you gave me just above do that, then i'm satisfied, thank you for your time and your help!

And about Ctrl+p / Ctrl+n i poorly explained and what i wanted to say was that without needing to config these binds (like you said on #80) with

bind '"\C-p": history-search-backward'
bind '"\C-n": history-search-forward'

it worked like natively as the way i wanted for arrows!
Idk if it comes from oh-my-bash or something but these binds are working!

Thanks again for your valuable help and time!

@NitramO-YT
Copy link
Author

What do you mean by "infinitely"? Doesn't that as many times as you pressed up instead of the infinite times? If it means really the infinite times, I cannot reproduce the behavior. If it means as many times as you have pressed up, it is another intentional change to the Readline behavior. The behavior is controlled by the action=ACTION option, which is also described in Manual §4.11.1. In Readline, those nsearch do not change the actual position in the command history while loading commands from the history. In contrast, ble.sh's nsearch implementation actually navigates through the history. If you want to use the Readline behavior of nsearch, you can specify action=load:

Look at this video:
Enregistrement d'écran_20241125_223322.webm
and look at the [number] "nsearch#[number]" at start i only go back in the history 15 times by arrow up, then i do Ctrl-c to quit, then i go back in the history one time, and go in the other way by arrow down, i can go in this way infinitely (like nsearch#200 but each line is empty)

@akinomyoga
Copy link
Owner

Sorry, i pasted the wrong lines, this wasn't working so i asked again and he gave me

Ah, I see.

To explain again what i wanted was: Navigating through the bash commands history with the arrows, without any visual info (like by default without ble.sh or oh-my-bash) and only need to press return once! So the lines you gave me just above do that, then i'm satisfied, thank you for your time and your help!

And about Ctrl+p / Ctrl+n i poorly explained and what i wanted to say was that without needing to config these binds (like you said on #80) with

Ah, yeah. That makes sense.

Look at this video:
Enregistrement d'écran_20241125_223322.webm
and look at the [number] "nsearch#[number]" at start i only go back in the history 15 times by arrow up, then i do Ctrl-c to quit, then i go back in the history one time, and go in the other way by arrow down, i can go in this way infinitely (like nsearch#200 but each line is empty)

OK, the behavior is indeed strange. The number after # is actually not the position but the number of search operations so far you have made, which decreases when you cancel the previous search operation. The up key in the forward nsearch mode is not the key to go up in the history, but the key to cancel the previous search operation. The position in the command history is the number after !, which does not change in your video. The present problem is that the search operation can be stacked infinitely even though it doesn't cause any state change. I'll take a look.

@akinomyoga akinomyoga added the question Question / Mis-usage label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Duplicate / Already fixed question Question / Mis-usage
Projects
None yet
Development

No branches or pull requests

2 participants