Skip to content

Commit 0a08d7e

Browse files
Lindenkomentic
authored andcommitted
Add unbind-default-keys config option
ref: helix-editor/helix#2720 ref: helix-editor/helix#2733
1 parent 3bff36a commit 0a08d7e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

book/src/remapping.md

+18
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,23 @@ Ctrl, Shift and Alt modifiers are encoded respectively with the prefixes
7575

7676
Keys can be disabled by binding them to the `no_op` command.
7777

78+
To remove all default bindings, `unbind-default-keys = true` can be added to the top level configuration.
79+
80+
```toml
81+
unbind-default-keys = true
82+
83+
# Only these normal mode bindings will be used
84+
[keys.normal]
85+
n = "normal_mode"
86+
t = "goto_definition"
87+
88+
# remember to add bindings to return to normal mode
89+
[keys.select]
90+
esc = "normal_mode"
91+
92+
[keys.insert]
93+
esc = "normal_mode"
94+
```
95+
7896
A list of commands is available in the [Keymap](https://docs.helix-editor.com/keymap.html) documentation
7997
and in the source code at [`helix-term/src/commands.rs`](https://github.com/helix-editor/helix/blob/master/helix-term/src/commands.rs) at the invocation of `static_commands!` macro and the `TypableCommandList`.

helix-term/src/config.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub struct Config {
2020
#[serde(deny_unknown_fields)]
2121
pub struct ConfigRaw {
2222
pub theme: Option<String>,
23+
#[serde(default)]
24+
pub unbind_default_keys: bool,
2325
pub keys: Option<HashMap<Mode, KeyTrie>>,
2426
pub editor: Option<toml::Value>,
2527
}
@@ -66,7 +68,10 @@ impl Config {
6668
local.and_then(|file| toml::from_str(&file).map_err(ConfigLoadError::BadConfig));
6769
let res = match (global_config, local_config) {
6870
(Ok(global), Ok(local)) => {
69-
let mut keys = keymap::default();
71+
let mut keys = match local.unbind_default_keys {
72+
true => HashMap::default(),
73+
false => keymap::default(),
74+
};
7075
if let Some(global_keys) = global.keys {
7176
merge_keys(&mut keys, global_keys)
7277
}
@@ -96,7 +101,10 @@ impl Config {
96101
return Err(ConfigLoadError::BadConfig(err))
97102
}
98103
(Ok(config), Err(_)) | (Err(_), Ok(config)) => {
99-
let mut keys = keymap::default();
104+
let mut keys = match config.unbind_default_keys {
105+
true => HashMap::default(),
106+
false => keymap::default(),
107+
};
100108
if let Some(keymap) = config.keys {
101109
merge_keys(&mut keys, keymap);
102110
}

helix-term/src/keymap.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ impl Keymaps {
318318
pub fn get(&mut self, mode: Mode, key: KeyEvent) -> KeymapResult {
319319
// TODO: remove the sticky part and look up manually
320320
let keymaps = &*self.map();
321-
let keymap = &keymaps[&mode];
321+
let keymap = match keymaps.get(&mode) {
322+
Some(keymap) => keymap,
323+
None => return KeymapResult::NotFound,
324+
};
322325

323326
if key!(Esc) == key {
324327
if !self.state.is_empty() {
@@ -373,7 +376,7 @@ impl Default for Keymaps {
373376
}
374377
}
375378

376-
/// Merge default config keys with user overwritten keys for custom user config.
379+
/// Merge existing config keys with user overwritten keys.
377380
pub fn merge_keys(dst: &mut HashMap<Mode, KeyTrie>, mut delta: HashMap<Mode, KeyTrie>) {
378381
for (mode, keys) in dst {
379382
keys.merge_nodes(

0 commit comments

Comments
 (0)