diff --git a/book/src/generated/static-cmd.md b/book/src/generated/static-cmd.md index d5ed908c60a1..8f96029423e1 100644 --- a/book/src/generated/static-cmd.md +++ b/book/src/generated/static-cmd.md @@ -65,6 +65,7 @@ | `page_cursor_half_up` | Move page and cursor half up | normal: `` ``, `` Z ``, `` z ``, `` Z ``, `` z ``, select: `` ``, `` Z ``, `` z ``, `` Z ``, `` z `` | | `page_cursor_half_down` | Move page and cursor half down | normal: `` ``, `` Z ``, `` z ``, `` Z ``, `` z ``, select: `` ``, `` Z ``, `` z ``, `` Z ``, `` z `` | | `select_all` | Select whole document | normal: `` % ``, select: `` % `` | +| `select_first_and_last_chars` | Select first and last characters of each selection | normal: `` ``, select: `` `` | | `select_regex` | Select all regex matches inside selections | normal: `` s ``, select: `` s `` | | `split_selection` | Split selections on regex matches | normal: `` S ``, select: `` S `` | | `split_selection_on_newline` | Split selection on newlines | normal: `` ``, select: `` `` | diff --git a/book/src/keymap.md b/book/src/keymap.md index 2797eaee2908..4de35ab09f14 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -117,6 +117,7 @@ Normal mode is the default mode when you launch helix. You can return to it from | `s` | Select all regex matches inside selections | `select_regex` | | `S` | Split selection into sub selections on regex matches | `split_selection` | | `Alt-s` | Split selection on newlines | `split_selection_on_newline` | +| `Alt-S` | Select first and last characters of each selection | `select_first_and_last_chars` | | `Alt-minus` | Merge selections | `merge_selections` | | `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` | | `&` | Align selection in columns | `align_selections` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index de02c905e4d1..36a2be0ead76 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -349,6 +349,7 @@ impl MappableCommand { page_cursor_half_up, "Move page and cursor half up", page_cursor_half_down, "Move page and cursor half down", select_all, "Select whole document", + select_first_and_last_chars, "Select first and last characters of each selection", select_regex, "Select all regex matches inside selections", split_selection, "Split selections on regex matches", split_selection_on_newline, "Split selection on newlines", @@ -2005,6 +2006,21 @@ fn select_all(cx: &mut Context) { doc.set_selection(view.id, Selection::single(0, end)) } +fn select_first_and_last_chars(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + + let selection = doc.selection(view.id).clone().transform_iter(|range| { + [ + Range::point(range.from()), + Range::point(graphemes::prev_grapheme_boundary(text, range.to())), + ] + .into_iter() + }); + + doc.set_selection(view.id, selection); +} + fn select_regex(cx: &mut Context) { let reg = cx.register.unwrap_or('/'); ui::regex_prompt( diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index c6cefd927574..8a7e0db29090 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -79,6 +79,7 @@ pub fn default() -> HashMap { "s" => select_regex, + "A-S" => select_first_and_last_chars, "A-s" => split_selection_on_newline, "A-minus" => merge_selections, "A-_" => merge_consecutive_selections,