Skip to content

fix(bash): fix and improve the keybinding to up #1515

New issue

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

By clicking “No 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? No Sign in to your account

Merged
merged 5 commits into from
Jan 7, 2024

Conversation

akinomyoga
Copy link
Contributor

This PR includes three different fixes related to the keybinding to up in Bash. The descriptions are found in commit messages and in code comments.

Copy link

vercel bot commented Jan 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
atuin-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 7, 2024 10:18pm

@akinomyoga
Copy link
Contributor Author

akinomyoga commented Jan 7, 2024

Now, the inlined script is so long that it might be better to consider outputting just the switches for C-r and up and moving the actual binding code to atuin.bash with the test for the switch. For example, we simplify fn init_bash(&self) as

let base = include_str!("../shell/atuin.bash");
let mut bind_ctrl_r = false;
let mut bind_up_arrow = false;
if std::env::var("ATUIN_NOBIND").is_err() {
    bind_ctrl_r = !self.disable_ctrl_r;
    bind_up_arrow = !self.disable_up_arrow;
}

println!("__atuin_bind_ctrl_r={bind_ctrl_r}");
println!("__atuin_bind_up_arrow={bind_up_arrow}");
println!("{base}");

and do branching in atuin.bash as

...

if [[ $__atuin_bind_ctrl_r == true ]]; then
    bind -m emacs -x '"\C-r": __atuin_history'
    ...
fi
if [[ $__atuin_bind_up_arrow == true ]]; then
    ...
fi
...

What do you think? (Edit: Commit da9df8a is an example implementation. I can drop the commit if you don't like it.)

@ellie
Copy link
Member

ellie commented Jan 7, 2024

I'm happy with this, thank you! Once the conflicts are resolved I'll get it merged

@akinomyoga
Copy link
Contributor Author

Thanks! I have rebased it!

ble.sh has a multiline mode where pressing [up] can be used to move to
the previous line.  The command history is loaded only when the [up]
key is pressed when the cursor is in the first line.  However, with
Atuin's integration, the [up] key always causes Atuin's history search
and cannot be used to move to the previous line when the cursor is not
in the first line.  There is also another situation that the [up] key
does not load the command history.

In this patch, with ble.sh, we perform Atuin's history search only in
the situation where the command history is loaded in the original
binding of ble.sh.
With the current implementation, the keybindings to [C-r] and [up] are
only set up in the currently activated keymaps in Bash.  As a result,
if the user changes the keymap after `eval "$(atuin init bash)"`,
Atuin's keybindings do not take effect.  In this patch, we bind
Atuin's keybindings in all the keymaps (emacs, vi-insert, and
vi-command) by explicitly specifying them (as done for the Zsh
integration).  The keybinding to "k" in the vi-command keymap is also
added to make it consistent with the Zsh integration.
In bash <= 4.2, "bind -x" with a key sequence with more than two bytes
does not work properly.  Inputting the key sequence will produce an
error message saying "bash: bash_execute_unix_command: cannot find
keymap for command", and the shell command is not executed.

To work around this, we can first translate the key sequences to
another key sequence with two bytes and run the shell command through
the two-byte key sequence.  In this patch, we use \C-x\C-p as the
two-byte key sequence.
ellie
ellie previously approved these changes Jan 7, 2024
Copy link
Member

@ellie ellie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Really appreciate all the improvements you've been doing in this area

@akinomyoga
Copy link
Contributor Author

Hmm, clippy requires me to rewrite

let mut bind_ctrl_r = false;
let mut bind_up_arrow = false;
if std::env::var("ATUIN_NOBIND").is_err() {
    bind_ctrl_r = !self.disable_ctrl_r;
    bind_up_arrow = !self.disable_up_arrow;
}

as

let mut bind_ctrl_r = false;
let bind_up_arrow = if std::env::var("ATUIN_NOBIND").is_err() {
    bind_ctrl_r = !self.disable_ctrl_r;
    !self.disable_up_arrow
} else {
    false
};

but this breaks the symmetry of bind_ctrl_r and bind_up_arrow.


OK, this time I can write it in this way:

let (bind_ctrl_r, bind_up_arrow) = if std::env::var("ATUIN_NOBIND").is_err() {
    (!self.disable_ctrl_r, !self.disable_up_arrow)
} else {
    (false, false)
};

though this wouldn't be the general solution in similar cases.

ellie
ellie previously approved these changes Jan 7, 2024
@akinomyoga
Copy link
Contributor Author

Probably it reads better when the condition is inversed. I've amended it.

Co-authored-by: Ellie Huxtable <ellie@elliehuxtable.com>
@ellie ellie merged commit 7dca752 into atuinsh:main Jan 7, 2024
@akinomyoga akinomyoga deleted the bash-up branch January 7, 2024 22:21
@akinomyoga
Copy link
Contributor Author

Thank you for all your help!

@atuin-bot
Copy link

This pull request has been mentioned on Atuin Community. There might be relevant details there:

https://forum.atuin.sh/t/up-arrow-error-commands-not-saved/339/3

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

Successfully merging this pull request may close these issues.

3 participants