diff --git a/book/src/generated/static-cmd.md b/book/src/generated/static-cmd.md index f6a82020f..449379d0f 100644 --- a/book/src/generated/static-cmd.md +++ b/book/src/generated/static-cmd.md @@ -153,6 +153,8 @@ | `goto_last_change` | Goto last change | normal: `` ]G ``, select: `` ]G `` | | `goto_line_start` | Goto line start | normal: `` gh ``, `` ``, select: `` gh ``, insert: `` `` | | `goto_line_end` | Goto line end | normal: `` gl ``, `` ``, select: `` gl `` | +| `goto_column` | Goto column | | +| `extend_to_column` | Extend to column | | | `goto_next_buffer` | Goto next buffer | normal: `` gn ``, select: `` gn `` | | `goto_previous_buffer` | Goto previous buffer | normal: `` gp ``, select: `` gp `` | | `goto_line_end_newline` | Goto newline at line end | insert: `` `` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 2669d8dd0..31651e8ba 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -451,6 +451,8 @@ impl MappableCommand { goto_last_change, "Goto last change", goto_line_start, "Goto line start", goto_line_end, "Goto line end", + goto_column, "Goto column", + extend_to_column, "Extend to column", goto_next_buffer, "Goto next buffer", goto_previous_buffer, "Goto previous buffer", goto_line_end_newline, "Goto newline at line end", @@ -3829,6 +3831,30 @@ fn goto_last_line_impl(cx: &mut Context, movement: Movement) { doc.set_selection(view.id, selection); } +fn goto_column(cx: &mut Context) { + goto_column_impl(cx, Movement::Move); +} + +fn extend_to_column(cx: &mut Context) { + goto_column_impl(cx, Movement::Extend); +} + +fn goto_column_impl(cx: &mut Context, movement: Movement) { + if let Some(count) = cx.count { + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + let selection = doc.selection(view.id).clone().transform(|range| { + let line = range.cursor_line(text); + let line_start = text.line_to_char(line); + let target = line_start + count.get(); + let line_end = line_end_char_index(&text, line); + let pos = graphemes::prev_grapheme_boundary(text, target).min(line_end); + range.put_cursor(text, pos, movement == Movement::Extend) + }); + doc.set_selection(view.id, selection); + } +} + fn goto_last_accessed_file(cx: &mut Context) { let view = view_mut!(cx.editor); if let Some(alt) = view.docs_access_history.pop() {