feat(tui): add configurable Vim keymap and startup default

Expose Vim bindings through the TUI keymap config and add a startup
toggle so Vim mode can be enabled by default.

Wire runtime keymap handling for Vim actions, add
`tui.vim_mode_default` to config parsing/runtime config, apply the
default in ChatWidget startup paths, and update docs/default
keymap/schema accordingly.
This commit is contained in:
Felipe Coury
2026-04-23 10:35:17 -03:00
committed by Felipe Coury
parent d49e77a7c2
commit cb3e5baa73
17 changed files with 1450 additions and 243 deletions
+264
View File
@@ -40,6 +40,8 @@ pub(crate) struct RuntimeKeymap {
pub(crate) chat: ChatKeymap,
pub(crate) composer: ComposerKeymap,
pub(crate) editor: EditorKeymap,
pub(crate) vim_normal: VimNormalKeymap,
pub(crate) vim_operator: VimOperatorKeymap,
pub(crate) pager: PagerKeymap,
pub(crate) list: ListKeymap,
pub(crate) approval: ApprovalKeymap,
@@ -52,6 +54,8 @@ pub(crate) struct AppKeymap {
pub(crate) open_transcript: Vec<KeyBinding>,
/// Open external editor for the current draft.
pub(crate) open_external_editor: Vec<KeyBinding>,
/// Toggle Vim mode for the composer input.
pub(crate) toggle_vim_mode: Vec<KeyBinding>,
}
#[derive(Clone, Debug)]
@@ -96,6 +100,48 @@ pub(crate) struct EditorKeymap {
pub(crate) yank: Vec<KeyBinding>,
}
#[derive(Clone, Debug)]
pub(crate) struct VimNormalKeymap {
pub(crate) enter_insert: Vec<KeyBinding>,
pub(crate) append_after_cursor: Vec<KeyBinding>,
pub(crate) append_line_end: Vec<KeyBinding>,
pub(crate) insert_line_start: Vec<KeyBinding>,
pub(crate) open_line_below: Vec<KeyBinding>,
pub(crate) open_line_above: Vec<KeyBinding>,
pub(crate) move_left: Vec<KeyBinding>,
pub(crate) move_right: Vec<KeyBinding>,
pub(crate) move_up: Vec<KeyBinding>,
pub(crate) move_down: Vec<KeyBinding>,
pub(crate) move_word_forward: Vec<KeyBinding>,
pub(crate) move_word_backward: Vec<KeyBinding>,
pub(crate) move_word_end: Vec<KeyBinding>,
pub(crate) move_line_start: Vec<KeyBinding>,
pub(crate) move_line_end: Vec<KeyBinding>,
pub(crate) delete_char: Vec<KeyBinding>,
pub(crate) delete_to_line_end: Vec<KeyBinding>,
pub(crate) yank_line: Vec<KeyBinding>,
pub(crate) paste_after: Vec<KeyBinding>,
pub(crate) start_delete_operator: Vec<KeyBinding>,
pub(crate) start_yank_operator: Vec<KeyBinding>,
pub(crate) cancel_operator: Vec<KeyBinding>,
}
#[derive(Clone, Debug)]
pub(crate) struct VimOperatorKeymap {
pub(crate) delete_line: Vec<KeyBinding>,
pub(crate) yank_line: Vec<KeyBinding>,
pub(crate) motion_left: Vec<KeyBinding>,
pub(crate) motion_right: Vec<KeyBinding>,
pub(crate) motion_up: Vec<KeyBinding>,
pub(crate) motion_down: Vec<KeyBinding>,
pub(crate) motion_word_forward: Vec<KeyBinding>,
pub(crate) motion_word_backward: Vec<KeyBinding>,
pub(crate) motion_word_end: Vec<KeyBinding>,
pub(crate) motion_line_start: Vec<KeyBinding>,
pub(crate) motion_line_end: Vec<KeyBinding>,
pub(crate) cancel: Vec<KeyBinding>,
}
/// Pager/overlay keybindings for transcript and static help views.
#[derive(Clone, Debug)]
pub(crate) struct PagerKeymap {
@@ -278,6 +324,11 @@ impl RuntimeKeymap {
&defaults.app.open_external_editor,
"tui.keymap.global.open_external_editor",
)?,
toggle_vim_mode: resolve_bindings(
keymap.global.toggle_vim_mode.as_ref(),
&defaults.app.toggle_vim_mode,
"tui.keymap.global.toggle_vim_mode",
)?,
};
let chat = ChatKeymap {
@@ -320,6 +371,61 @@ impl RuntimeKeymap {
yank: resolve_local!(keymap, defaults, editor, yank),
};
let vim_normal = VimNormalKeymap {
enter_insert: resolve_local!(keymap, defaults, vim_normal, enter_insert),
append_after_cursor: resolve_local!(keymap, defaults, vim_normal, append_after_cursor),
append_line_end: resolve_local!(keymap, defaults, vim_normal, append_line_end),
insert_line_start: resolve_local!(keymap, defaults, vim_normal, insert_line_start),
open_line_below: resolve_local!(keymap, defaults, vim_normal, open_line_below),
open_line_above: resolve_local!(keymap, defaults, vim_normal, open_line_above),
move_left: resolve_local!(keymap, defaults, vim_normal, move_left),
move_right: resolve_local!(keymap, defaults, vim_normal, move_right),
move_up: resolve_local!(keymap, defaults, vim_normal, move_up),
move_down: resolve_local!(keymap, defaults, vim_normal, move_down),
move_word_forward: resolve_local!(keymap, defaults, vim_normal, move_word_forward),
move_word_backward: resolve_local!(keymap, defaults, vim_normal, move_word_backward),
move_word_end: resolve_local!(keymap, defaults, vim_normal, move_word_end),
move_line_start: resolve_local!(keymap, defaults, vim_normal, move_line_start),
move_line_end: resolve_local!(keymap, defaults, vim_normal, move_line_end),
delete_char: resolve_local!(keymap, defaults, vim_normal, delete_char),
delete_to_line_end: resolve_local!(keymap, defaults, vim_normal, delete_to_line_end),
yank_line: resolve_local!(keymap, defaults, vim_normal, yank_line),
paste_after: resolve_local!(keymap, defaults, vim_normal, paste_after),
start_delete_operator: resolve_local!(
keymap,
defaults,
vim_normal,
start_delete_operator
),
start_yank_operator: resolve_local!(keymap, defaults, vim_normal, start_yank_operator),
cancel_operator: resolve_local!(keymap, defaults, vim_normal, cancel_operator),
};
let vim_operator = VimOperatorKeymap {
delete_line: resolve_local!(keymap, defaults, vim_operator, delete_line),
yank_line: resolve_local!(keymap, defaults, vim_operator, yank_line),
motion_left: resolve_local!(keymap, defaults, vim_operator, motion_left),
motion_right: resolve_local!(keymap, defaults, vim_operator, motion_right),
motion_up: resolve_local!(keymap, defaults, vim_operator, motion_up),
motion_down: resolve_local!(keymap, defaults, vim_operator, motion_down),
motion_word_forward: resolve_local!(
keymap,
defaults,
vim_operator,
motion_word_forward
),
motion_word_backward: resolve_local!(
keymap,
defaults,
vim_operator,
motion_word_backward
),
motion_word_end: resolve_local!(keymap, defaults, vim_operator, motion_word_end),
motion_line_start: resolve_local!(keymap, defaults, vim_operator, motion_line_start),
motion_line_end: resolve_local!(keymap, defaults, vim_operator, motion_line_end),
cancel: resolve_local!(keymap, defaults, vim_operator, cancel),
};
let pager = PagerKeymap {
scroll_up: resolve_local!(keymap, defaults, pager, scroll_up),
scroll_down: resolve_local!(keymap, defaults, pager, scroll_down),
@@ -369,6 +475,8 @@ impl RuntimeKeymap {
chat,
composer,
editor,
vim_normal,
vim_operator,
pager,
list,
approval,
@@ -395,6 +503,7 @@ impl RuntimeKeymap {
app: AppKeymap {
open_transcript: default_bindings![ctrl(KeyCode::Char('t'))],
open_external_editor: default_bindings![ctrl(KeyCode::Char('g'))],
toggle_vim_mode: default_bindings![],
},
chat: ChatKeymap {
edit_previous_message: default_bindings![plain(KeyCode::Esc)],
@@ -449,6 +558,62 @@ impl RuntimeKeymap {
kill_line_end: default_bindings![ctrl(KeyCode::Char('k'))],
yank: default_bindings![ctrl(KeyCode::Char('y'))],
},
vim_normal: VimNormalKeymap {
enter_insert: default_bindings![plain(KeyCode::Char('i'))],
append_after_cursor: default_bindings![plain(KeyCode::Char('a'))],
append_line_end: default_bindings![
shift(KeyCode::Char('a')),
plain(KeyCode::Char('A'))
],
insert_line_start: default_bindings![
shift(KeyCode::Char('i')),
plain(KeyCode::Char('I'))
],
open_line_below: default_bindings![plain(KeyCode::Char('o'))],
open_line_above: default_bindings![
shift(KeyCode::Char('o')),
plain(KeyCode::Char('O'))
],
move_left: default_bindings![plain(KeyCode::Char('h'))],
move_right: default_bindings![plain(KeyCode::Char('l'))],
move_up: default_bindings![plain(KeyCode::Char('k'))],
move_down: default_bindings![plain(KeyCode::Char('j'))],
move_word_forward: default_bindings![plain(KeyCode::Char('w'))],
move_word_backward: default_bindings![plain(KeyCode::Char('b'))],
move_word_end: default_bindings![plain(KeyCode::Char('e'))],
move_line_start: default_bindings![plain(KeyCode::Char('0'))],
move_line_end: default_bindings![
plain(KeyCode::Char('$')),
shift(KeyCode::Char('$'))
],
delete_char: default_bindings![plain(KeyCode::Char('x'))],
delete_to_line_end: default_bindings![
shift(KeyCode::Char('d')),
plain(KeyCode::Char('D'))
],
yank_line: default_bindings![shift(KeyCode::Char('y')), plain(KeyCode::Char('Y'))],
paste_after: default_bindings![plain(KeyCode::Char('p'))],
start_delete_operator: default_bindings![plain(KeyCode::Char('d'))],
start_yank_operator: default_bindings![plain(KeyCode::Char('y'))],
cancel_operator: default_bindings![plain(KeyCode::Esc)],
},
vim_operator: VimOperatorKeymap {
delete_line: default_bindings![plain(KeyCode::Char('d'))],
yank_line: default_bindings![plain(KeyCode::Char('y'))],
motion_left: default_bindings![plain(KeyCode::Char('h'))],
motion_right: default_bindings![plain(KeyCode::Char('l'))],
motion_up: default_bindings![plain(KeyCode::Char('k'))],
motion_down: default_bindings![plain(KeyCode::Char('j'))],
motion_word_forward: default_bindings![plain(KeyCode::Char('w'))],
motion_word_backward: default_bindings![plain(KeyCode::Char('b'))],
motion_word_end: default_bindings![plain(KeyCode::Char('e'))],
motion_line_start: default_bindings![plain(KeyCode::Char('0'))],
motion_line_end: default_bindings![
plain(KeyCode::Char('$')),
shift(KeyCode::Char('$'))
],
cancel: default_bindings![plain(KeyCode::Esc)],
},
pager: PagerKeymap {
scroll_up: default_bindings![plain(KeyCode::Up), plain(KeyCode::Char('k'))],
scroll_down: default_bindings![plain(KeyCode::Down), plain(KeyCode::Char('j'))],
@@ -551,6 +716,7 @@ impl RuntimeKeymap {
"open_external_editor",
self.app.open_external_editor.as_slice(),
),
("toggle_vim_mode", self.app.toggle_vim_mode.as_slice()),
(
"edit_previous_message",
self.chat.edit_previous_message.as_slice(),
@@ -570,6 +736,7 @@ impl RuntimeKeymap {
"open_external_editor",
self.app.open_external_editor.as_slice(),
),
("toggle_vim_mode", self.app.toggle_vim_mode.as_slice()),
("composer.submit", self.composer.submit.as_slice()),
("composer.queue", self.composer.queue.as_slice()),
(
@@ -607,6 +774,103 @@ impl RuntimeKeymap {
],
)?;
validate_unique(
"vim_normal",
[
("enter_insert", self.vim_normal.enter_insert.as_slice()),
(
"append_after_cursor",
self.vim_normal.append_after_cursor.as_slice(),
),
(
"append_line_end",
self.vim_normal.append_line_end.as_slice(),
),
(
"insert_line_start",
self.vim_normal.insert_line_start.as_slice(),
),
(
"open_line_below",
self.vim_normal.open_line_below.as_slice(),
),
(
"open_line_above",
self.vim_normal.open_line_above.as_slice(),
),
("move_left", self.vim_normal.move_left.as_slice()),
("move_right", self.vim_normal.move_right.as_slice()),
("move_up", self.vim_normal.move_up.as_slice()),
("move_down", self.vim_normal.move_down.as_slice()),
(
"move_word_forward",
self.vim_normal.move_word_forward.as_slice(),
),
(
"move_word_backward",
self.vim_normal.move_word_backward.as_slice(),
),
("move_word_end", self.vim_normal.move_word_end.as_slice()),
(
"move_line_start",
self.vim_normal.move_line_start.as_slice(),
),
("move_line_end", self.vim_normal.move_line_end.as_slice()),
("delete_char", self.vim_normal.delete_char.as_slice()),
(
"delete_to_line_end",
self.vim_normal.delete_to_line_end.as_slice(),
),
("yank_line", self.vim_normal.yank_line.as_slice()),
("paste_after", self.vim_normal.paste_after.as_slice()),
(
"start_delete_operator",
self.vim_normal.start_delete_operator.as_slice(),
),
(
"start_yank_operator",
self.vim_normal.start_yank_operator.as_slice(),
),
(
"cancel_operator",
self.vim_normal.cancel_operator.as_slice(),
),
],
)?;
validate_unique(
"vim_operator",
[
("delete_line", self.vim_operator.delete_line.as_slice()),
("yank_line", self.vim_operator.yank_line.as_slice()),
("motion_left", self.vim_operator.motion_left.as_slice()),
("motion_right", self.vim_operator.motion_right.as_slice()),
("motion_up", self.vim_operator.motion_up.as_slice()),
("motion_down", self.vim_operator.motion_down.as_slice()),
(
"motion_word_forward",
self.vim_operator.motion_word_forward.as_slice(),
),
(
"motion_word_backward",
self.vim_operator.motion_word_backward.as_slice(),
),
(
"motion_word_end",
self.vim_operator.motion_word_end.as_slice(),
),
(
"motion_line_start",
self.vim_operator.motion_line_start.as_slice(),
),
(
"motion_line_end",
self.vim_operator.motion_line_end.as_slice(),
),
("cancel", self.vim_operator.cancel.as_slice()),
],
)?;
validate_unique(
"pager",
[