From 0d1dd4603ee731cbca9e204a9a97df492f6a89b5 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Tue, 9 Nov 2021 15:55:50 +0800 Subject: [PATCH] readline style insert mode --- book/src/keymap.md | 13 +++++++++++++ helix-term/src/commands.rs | 31 +++++++++++++++++++++++++++++++ helix-term/src/keymap.rs | 16 ++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/book/src/keymap.md b/book/src/keymap.md index 6ae1e8d47..3e73855b7 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -239,6 +239,19 @@ ## Insert Mode | `Escape` | Switch to normal mode | `normal_mode` | | `Ctrl-x` | Autocomplete | `completion` | | `Ctrl-w` | Delete previous word | `delete_word_backward` | +| `Alt-b`, `Alt-Left` | Backward a word | +| `Ctrl-b`, `Left` | Backward a char | +| `Alt-f`, `Alt-Right` | Forward a word | +| `Ctrl-f`, `Right` | Forward a char | +| `Ctrl-e`, `End` | move to line end | +| `Ctrl-a`, `Home` | move to line start | +| `Ctrl-w` | delete previous word | +| `Ctrl-u` | delete to start of line | +| `Ctrl-k` | delete to end of line | +| `backspace`, `Ctrl-h` | delete previous char | +| `delete`, `Ctrl-d` | delete previous char | +| `Ctrl-p`, `Up` | move to previous line | +| `Ctrl-n`, `Down` | move to next line | ## Select / extend mode diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 42163b8e0..bdb875ad8 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -185,6 +185,7 @@ pub fn doc(&self) -> &'static str { copy_selection_on_prev_line, "Copy selection on previous line", move_next_word_start, "Move to beginning of next word", move_prev_word_start, "Move to beginning of previous word", + move_prev_word_end, "Move to end of previous word", move_next_word_end, "Move to end of next word", move_next_long_word_start, "Move to beginning of next long word", move_prev_long_word_start, "Move to beginning of previous long word", @@ -279,6 +280,9 @@ pub fn doc(&self) -> &'static str { delete_char_backward, "Delete previous char", delete_char_forward, "Delete next char", delete_word_backward, "Delete previous word", + delete_word_forward, "Delete next word", + kill_to_line_start, "Delete content till the start of the line", + kill_to_line_end, "Delete content till the end of the line", undo, "Undo change", redo, "Redo change", yank, "Yank selection", @@ -563,6 +567,16 @@ fn extend_to_line_start(cx: &mut Context) { goto_line_start_impl(view, doc, Movement::Extend) } +fn kill_to_line_start(cx: &mut Context) { + extend_to_line_start(cx); + delete_selection(cx); +} + +fn kill_to_line_end(cx: &mut Context) { + extend_to_line_end(cx); + delete_selection(cx); +} + fn goto_first_nonwhitespace(cx: &mut Context) { let (view, doc) = current!(cx.editor); let text = doc.text().slice(..); @@ -639,6 +653,10 @@ fn move_prev_word_start(cx: &mut Context) { move_word_impl(cx, movement::move_prev_word_start) } +fn move_prev_word_end(cx: &mut Context) { + move_word_impl(cx, movement::move_prev_word_end) +} + fn move_next_word_end(cx: &mut Context) { move_word_impl(cx, movement::move_next_word_end) } @@ -3802,6 +3820,19 @@ pub fn delete_word_backward(cx: &mut Context) { doc.set_selection(view.id, selection); delete_selection(cx) } + + pub fn delete_word_forward(cx: &mut Context) { + let count = cx.count(); + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + + let selection = doc + .selection(view.id) + .clone() + .transform(|range| movement::move_next_word_start(text, range, count)); + doc.set_selection(view.id, selection); + delete_selection(cx) + } } // Undo / Redo diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index d497401f9..f8d70685c 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -651,19 +651,35 @@ fn default() -> Keymaps { "esc" => normal_mode, "backspace" => delete_char_backward, + "C-h" => delete_char_backward, "del" => delete_char_forward, + "C-d" => delete_char_forward, "ret" => insert_newline, "tab" => insert_tab, "C-w" => delete_word_backward, + "A-d" => delete_word_forward, "left" => move_char_left, + "C-b" => move_char_left, "down" => move_line_down, + "C-n" => move_line_down, "up" => move_line_up, + "C-p" => move_line_up, "right" => move_char_right, + "C-f" => move_char_right, + "A-b" => move_prev_word_end, + "A-left" => move_prev_word_end, + "A-f" => move_next_word_start, + "A-right" => move_next_word_start, "pageup" => page_up, "pagedown" => page_down, "home" => goto_line_start, + "C-a" => goto_line_start, "end" => goto_line_end_newline, + "C-e" => goto_line_end_newline, + + "C-k" => kill_to_line_end, + "C-u" => kill_to_line_start, "C-x" => completion, });