Skip to content

Commit e28abc9

Browse files
dbowringkumawatdarshan
authored andcommitted
Add goto_column and extend_to_column commands (#13440)
1 parent 313f577 commit e28abc9

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

book/src/generated/static-cmd.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
| `goto_last_change` | Goto last change | normal: `` ]G ``, select: `` ]G `` |
154154
| `goto_line_start` | Goto line start | normal: `` gh ``, `` <home> ``, select: `` gh ``, insert: `` <home> `` |
155155
| `goto_line_end` | Goto line end | normal: `` gl ``, `` <end> ``, select: `` gl `` |
156+
| `goto_column` | Goto column | normal: `` g\| `` |
157+
| `extend_to_column` | Extend to column | select: `` g\| `` |
156158
| `goto_next_buffer` | Goto next buffer | normal: `` gn ``, select: `` gn `` |
157159
| `goto_previous_buffer` | Goto previous buffer | normal: `` gp ``, select: `` gp `` |
158160
| `goto_line_end_newline` | Goto newline at line end | insert: `` <end> `` |

book/src/keymap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Jumps to various locations.
213213
| Key | Description | Command |
214214
| ----- | ----------- | ------- |
215215
| `g` | Go to line number `<n>` else start of file | `goto_file_start` |
216+
| <code>&#124;</code> | Go to column number `<n>` else start of line | `goto_column` |
216217
| `e` | Go to the end of the file | `goto_last_line` |
217218
| `f` | Go to files in the selections | `goto_file` |
218219
| `h` | Go to the start of the line | `goto_line_start` |

helix-term/src/commands.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ impl MappableCommand {
451451
goto_last_change, "Goto last change",
452452
goto_line_start, "Goto line start",
453453
goto_line_end, "Goto line end",
454+
goto_column, "Goto column",
455+
extend_to_column, "Extend to column",
454456
goto_next_buffer, "Goto next buffer",
455457
goto_previous_buffer, "Goto previous buffer",
456458
goto_line_end_newline, "Goto newline at line end",
@@ -3829,6 +3831,28 @@ fn goto_last_line_impl(cx: &mut Context, movement: Movement) {
38293831
doc.set_selection(view.id, selection);
38303832
}
38313833

3834+
fn goto_column(cx: &mut Context) {
3835+
goto_column_impl(cx, Movement::Move);
3836+
}
3837+
3838+
fn extend_to_column(cx: &mut Context) {
3839+
goto_column_impl(cx, Movement::Extend);
3840+
}
3841+
3842+
fn goto_column_impl(cx: &mut Context, movement: Movement) {
3843+
let count = cx.count();
3844+
let (view, doc) = current!(cx.editor);
3845+
let text = doc.text().slice(..);
3846+
let selection = doc.selection(view.id).clone().transform(|range| {
3847+
let line = range.cursor_line(text);
3848+
let line_start = text.line_to_char(line);
3849+
let line_end = line_end_char_index(&text, line);
3850+
let pos = graphemes::nth_next_grapheme_boundary(text, line_start, count - 1).min(line_end);
3851+
range.put_cursor(text, pos, movement == Movement::Extend)
3852+
});
3853+
doc.set_selection(view.id, selection);
3854+
}
3855+
38323856
fn goto_last_accessed_file(cx: &mut Context) {
38333857
let view = view_mut!(cx.editor);
38343858
if let Some(alt) = view.docs_access_history.pop() {

helix-term/src/keymap/default.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
3838
"G" => goto_line,
3939
"g" => { "Goto"
4040
"g" => goto_file_start,
41+
"|" => goto_column,
4142
"e" => goto_last_line,
4243
"f" => goto_file,
4344
"h" => goto_line_start,
@@ -368,6 +369,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
368369
"v" => normal_mode,
369370
"g" => { "Goto"
370371
"g" => extend_to_file_start,
372+
"|" => extend_to_column,
371373
"e" => extend_to_last_line,
372374
"k" => extend_line_up,
373375
"j" => extend_line_down,

0 commit comments

Comments
 (0)