Repository navigation
fix(config): match unnamed keybindings by the key they bind - #18828
Conversation
Assigning `$env.config.keybindings` merges the incoming list into the existing one, and matching on `name` alone left unnamed bindings matching nothing: re-sourcing a config grew the list every time, 9 entries to 10 to 12. Modifier, keycode and mode are the identity reedline already keys a binding by.
Both entries claimed a full list replacement (`=`) clears the defaults. It has not since nushell#18747 made them `Config::default()` values merged by name, so `$env.config.keybindings = []` and `$env.config.menus = []` are both no-ops. Dropped those two commented examples rather than document an operation that does nothing.
|
Hey, just a bot checking in! You edited files related to the configuration. |
|
Makes sense. Thanks. |
|
I considered opening a PR to update the docs, but I'm still debating whether this behavior is the right idea:
|
|
@fdncred What do you think about that? There is no way that I would agree with removing But adding it to the docs is a must if we keep it that way. |
|
After thinking about this more, here are my thoughts. We probably shouldn't have special handling with how append versus overwrite works. It should be normal nushell conventions,
{
name: reload_config
modifier: none
keycode: f5
mode: [emacs vi_normal vi_insert]
event: {
send: executehostcommand,
cmd: $"source '($nu.env-path)';source '($nu.config-path)'"
}
}
I think I've hit all the points. If I've missed some, please point it out and I'll respond. |
|
Agree with basically everything you said! I definitely don't want to break Regarding 4. if you plan to unique/key off of To add some context of why this was an issue in my config, my Setting |
…19418) ## Description Since #18761 assigning `$env.config.keybindings` or `$env.config.menus` merges into the current list in the config layer. That made the two lists behave unlike every other list in nushell: `=` could not drop a single binding, `menus = []` cleared nothing, and a binding with a bad keycode errored on every prompt until the whole list was cleared. #18828 and #18870 patched the merge (unnamed bindings, then name plus key, with a warning for atuin's shared names in #18848), but each patch added a rule on top of the merge. This PR takes the merge out. Both lists are stored as written, like any other list: ```nu $env.config.keybindings = [...] # replaces the list $env.config.keybindings ++= [...] # appends to it $env.config.keybindings | where name != x # filters it $env.config.keybindings | reverse | uniq-by modifier keycode mode | reverse # drops exact repeats ``` Overlaps are resolved where the list is used, by reedline, with one rule for both lists: entries are applied in order and the last one wins. | | spot | free spot | taken spot | |---|---|---|---| | keybinding | mode, modifier, keycode | filled | replaced | | menu | name | appended | replaced in place | Keys already worked like this in reedline, since `add_binding` is a `HashMap::insert`. Menus did not, since the lookup by name takes the first match, so nushell/reedline#1294 made a later menu replace one of the same name. A keybinding's `name` is only a label and plays no part in what a key does. `SharedKeybindingName` goes, since one name on several keys (atuin) is no longer ambiguous once nothing merges by name. The `KeyIdentity` machinery that mirrored reedline's key parsing goes with it. For keybindings there is a test where nushell hands the list over: `create_keybindings` is checked for last-wins, for `event: null` removing a reedline base binding, and for a later entry covering fewer modes. Menus rely on the reedline test for the same rule, since nushell only passes the list on. ## User-facing changes (Release notes) ### Assigning `$env.config.keybindings` and `$env.config.menus` replaces the list Breaking: `$env.config.keybindings = [...]` and `$env.config.menus = [...]` now replace the whole list, including nushell's default menus and the keys that open them (Tab and Shift+Tab completion, Ctrl+Space, F1 help, Ctrl+X and Ctrl+Z paging, the Ctrl+R history menu). If your config assigns either list, or sets `keybindings` or `menus` inside a `$env.config = {...}` record, switch to `++=` or Tab stops completing. Reedline's own bindings (movement, editing) are not part of the list and stay. ```nu $env.config.keybindings ++= [{ name: my_binding, modifier: control, keycode: char_t, mode: emacs, event: { send: clearscreen } }] ``` To get the defaults back in a running shell, assign them again from a clean `nu -n`: ```nu $env.config.keybindings = (^$nu.current-exe -n -c '$env.config.keybindings | to nuon' | from nuon) $env.config.menus = (^$nu.current-exe -n -c '$env.config.menus | to nuon' | from nuon) ``` Filtering a list now removes what it drops, and `= []` clears either list. When several keybindings bind the same key in the same mode, or several menus share a name, the last one wins. A keybinding's `name` is only a label, so sharing one between keys no longer warns. To unbind a key that reedline itself binds, add an entry for it with `event: null`. ## Additional notes Menus need nushell/reedline#1294, which is in the reedline bump commit before the menus commit. The book still describes the merge in `line_editor.md` (the keybindings section and the menus note), so that needs a follow-up PR to nushell.github.io. Related: #18761, #18828, #18870, #18848.
Description
#18761 swapped the wholesale
self.keybindings = keybindingsassignment for a loop that merges each incoming binding into the existing list, matching onname.nameisOption<Value>onParsedKeybinding, so an unnamed binding matches nothing and falls through to thepush, every time.Re-sourcing a config therefore grows the list by one entry per unnamed binding on each pass.
The fix matches on the identity reedline already keys a binding by: named bindings compare on the expanded
name, unnamed ones onmodifier,keycodeandmode, and a named/unnamed pair never matches.Also fixes
doc_config.nu, which still claimed a full list replacement (=) clears the defaults forkeybindingsandmenus.That was true when #18747 wrote it, but #18761 made both assignments no-ops without updating the text, so I dropped the two commented examples.
Worth landing before 0.115, since neither PR has shipped yet (last tag is 0.114.1).
User-facing changes (Release notes)
Fixed config reloads duplicating unnamed keybindings
Assigning
$env.config.keybindingsor$env.config.menusnow merges into the defaults rather than replacing them, so$env.config.keybindings = []and$env.config.menus = []no longer clear anything.Set
event: nullon a matching binding to unbind a key.Additional notes
The merge change rode in on #18761 alongside a large amount of unrelated completer work, which is probably why the doc drift went unnoticed.
Out of scope: the merge still has no way to drop a binding outright.