Skip to content

fix(config): match unnamed keybindings by the key they bind - #18828

Merged
fdncred merged 2 commits into
nushell:mainfrom
kronberger-droid:fix-keybinding-merge-duplication
Aug 13, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
kronberger-droid:fix-keybinding-merge-duplication

Conversation

@kronberger-droid

@kronberger-droid kronberger-droid commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

#18761 swapped the wholesale self.keybindings = keybindings assignment for a loop that merges each incoming binding into the existing list, matching on name.
name is Option<Value> on ParsedKeybinding, so an unnamed binding matches nothing and falls through to the push, 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 on modifier, keycode and mode, and a named/unnamed pair never matches.

Also fixes doc_config.nu, which still claimed a full list replacement (=) clears the defaults for keybindings and menus.
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.keybindings or $env.config.menus now merges into the defaults rather than replacing them, so $env.config.keybindings = [] and $env.config.menus = [] no longer clear anything.
Set event: null on 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.

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.
@github-actions

Copy link
Copy Markdown

Hey, just a bot checking in! You edited files related to the configuration.
If you changed any of the default values or added a new config option, don't forget to update the doc_config.nu which documents the options for our users including the defaults provided by the Rust implementation.
If you didn't make a change here, you can just ignore me.

@fdncred
fdncred merged commit 62e7069 into nushell:main Aug 13, 2026
16 checks passed
@fdncred

fdncred commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Makes sense. Thanks.

@null-dev

null-dev commented Sep 7, 2026 •

Copy link
Copy Markdown

$env.config.keybindings and $env.config.menus being variables with special behavior is surprising to me because it's not documented anywhere except in the comments of the default generated config file (crates/nu-config/default_files/doc_config.nu). Since I don't re-generate my config file when I upgrade nushell (I doubt anybody does), I never saw the updated comment.

I considered opening a PR to update the docs, but I'm still debating whether this behavior is the right idea:

  • Does it make sense to make the variables in $env.config behave differently, just to support source env.nu ; source config.nu?
  • Is source env.nu ; source config.nu even the recommended way to reload my config? Searching https://www.nushell.sh/book/ for "reload config" yields no results.
  • Why not add a truly idempotent config reload command that first clears the old $env.config before sourcing the new config, instead of modifying the normal behavior of some variables to try to make source env.nu ; source config.nu idempotent?

@kronberger-droid

Copy link
Copy Markdown
Contributor Author

@fdncred What do you think about that?
I mean its kind of true, but I would lean more towards explicitly stating when I want to overwrite a value:
$env.config.keybindings = []
or when I want to append to it:
$env.config.keybindings += []
or something similar.

There is no way that I would agree with removing source .. tho.

But adding it to the docs is a must if we keep it that way.

@fdncred

fdncred commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

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, = overwrites, += appends.

  1. I've had this in my config for years. So, I think it should continue to work.
  {
    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)'"
    }
  }
  1. $env.config.keybindings = [] (with literlly an empty list) should clear all keybindings defined in the config.nu file (or loaded from others), essentially zeroing them out. There are other keybindings that are not set-able that it shouldn't clear.
  2. If I define some keybinding with $env.config.keybindings = [some keybinding] - this should clear all keybindings defined in config files and only have the one you just set. That's how nushell works $var = [blah] shadows whatever mut $var was.
  3. $env.config.keybindings += [] (with literally an empty list) should be a no-op. However, $env.config.keybindings += [some keybinding] should append the keybinding. The question still remains I guess is what makes a keybinding unique so it's appended vs overwriting an existing one. Is it the name? Is it the name, modifier, and keycode? Is it just the modifier, and keycode?
  4. I'd be fine with a rehash / config reload type of command but it would have to be more than just env.nu and config.nu. It would have to mimic the startup and load all files including autoloads.

I think I've hit all the points. If I've missed some, please point it out and I'll respond.

@null-dev

null-dev commented Sep 11, 2026 •

Copy link
Copy Markdown

Agree with basically everything you said! I definitely don't want to break source env.nu ; source config.nu, I was only concerned about the idea of making some variables behave in a special way just so that source ... behaves more idempotent. Many other shells also advise users reload their config with source ..., but AFAIK none of them needed to add any special behavior...

Regarding 4. if you plan to unique/key off of (modifier, keycode), I would include mode as well, since a user might want the same key to do different things in insert or normal mode. But IMO just enforcing a unique name is fine, having overlapping keybinds is not a big deal. Un-named keybindings can be discouraged, but still supported with no de-duplication/uniqueness constraints enforced on them.

To add some context of why this was an issue in my config, my config.nu looks something like this:

source autogenerated/atuin.nu
# Remove atuin override of nushell's default "up arrow" keybinding
$env.config.keybindings = $env.config.keybindings | where not ($it.name == "atuin_up_arrow")

Setting event: null on the keybind doesn't work around the issue because that completely disables the "up arrow" without restoring Nushell's default behavior. That said, I now see that I can pass --disable-up-arrow into atuin when making it generate it's nushell integration script, so this is not really an issue for me anymore.

@kronberger-droid
kronberger-droid deleted the fix-keybinding-merge-duplication branch September 14, 2026 21:36
fdncred pushed a commit that referenced this pull request Nov 4, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants