From 8d62f0f3400a535df1ec236f651b36a2f2961343 Mon Sep 17 00:00:00 2001 From: Callie Peden Date: Sat, 26 Sep 2026 03:12:38 -0400 Subject: [PATCH] feat(tui): Jump word navigation when ctrl/alt is held - Bound Ctrl/Alt+Left and Ctrl/Alt+Right to word navigation in the chat prompt. --- cli/internal/tui/chat.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cli/internal/tui/chat.go b/cli/internal/tui/chat.go index f5c86adf..1a338279 100644 --- a/cli/internal/tui/chat.go +++ b/cli/internal/tui/chat.go @@ -342,6 +342,8 @@ func NewChatModel(session *daemon.Session, client *daemon.ChatClient) ChatModel return strings.Repeat(" ", promptMarkWidth) }) ta.KeyMap.InsertNewline.SetKeys("enter", "ctrl+m", "alt+enter", "shift+enter") + ta.KeyMap.WordBackward.SetKeys("alt+left", "alt+b", "ctrl+left") + ta.KeyMap.WordForward.SetKeys("alt+right", "alt+f", "ctrl+right") ta.SetHeight(6) ta.FocusedStyle.Prompt = DefaultStyles.Prompt ta.FocusedStyle.CursorLine = lipgloss.NewStyle() @@ -429,6 +431,25 @@ func (m *ChatModel) ClearNotices() { m.syncViewportHeight() } +// wordBackwardAtStart reports whether the prompt has no word before its cursor. +// The textarea's word-backward handler loops indefinitely in this case. +func (m ChatModel) wordBackwardAtStart() bool { + lines := strings.Split(m.TextArea.Value(), "\n") + row := m.TextArea.Line() + if row < 0 || row >= len(lines) { + return false + } + col := m.TextArea.LineInfo().StartColumn + m.TextArea.LineInfo().ColumnOffset + current := []rune(lines[row]) + col = max(0, min(col, len(current))) + for _, line := range lines[:row] { + if strings.TrimSpace(line) != "" { + return false + } + } + return strings.TrimSpace(string(current[:col])) == "" +} + func (m ChatModel) promptLines() int { if m.WorkspaceRecovery != nil { return 0 @@ -1140,6 +1161,14 @@ func (m ChatModel) update(msg tea.Msg) (ChatModel, tea.Cmd) { return m, nil } + // Bubbles wordLeft never terminates when everything before the cursor + // is whitespace. Move to the input start directly in that case. + if msg.Type == tea.KeyCtrlLeft && m.wordBackwardAtStart() { + var cmd tea.Cmd + m.TextArea, cmd = m.TextArea.Update(tea.KeyMsg{Type: tea.KeyCtrlHome}) + return m, cmd + } + if msg.Type == tea.KeyEnter && !msg.Alt { trimmed := strings.TrimSpace(m.TextArea.Value()) if trimmed != "" { -- knotmirror 0.0.1