From 9c61384e883fac8314cc3ea0268299fc2e0ea85b Mon Sep 17 00:00:00 2001 From: Oleksii Orlenko Date: Thu, 13 Aug 2026 12:34:28 +0000 Subject: [PATCH] helix: Add `vim::HelixGotoLine` action and bind it to `G` (#61581) # Objective Make `G` keybinding in Helix mode work like in Helix and not like in Vim. Fixes https://github.com/zed-industries/zed/issues/61580 ## Solution Helix has two ways to jump to a line by line number. One is the `goto_file_start` command (bound to `gg`) that optionally takes a count to go to that line instead of the start of the file. Zed already supports it as `vim::StartOfDocument`. The other is the dedicated `goto_line` command (bound to `G`) that only does that and nothing else. Zed did not have it. What's worse, the default `"shift-g": "vim::EndOfDocument"` binding leaked from Vim keymap into Helix keymap, which previously made `G` accidentally work in Helix mode for the wrong reason, until https://github.com/zed-industries/zed/pull/59449 fixed the behavior of `vim::StartOfDocument` and `vim::EndOfDocument` actions to match Helix exactly. This broke `G` and exposed that `G` was bound to the wrong action in Helix mode, and the correct one didn't exist. This PR fixes that in the following way: - adds new`vim::HelixGotoLine` action - binds it to `shift-g` in `helix_normal` and `helix_select` modes in the default Vim keymap ## Testing - Unit tests - Manual testing ## Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards ([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) and [icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md) guidelines) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable --- Release Notes: - Fixed the behavior of `G` binding in Helix mode and added new `vim::HelixGotoLine` action Signed-off-by: Oleksii Orlenko --- assets/keymaps/vim.json | 1 + crates/vim/src/helix.rs | 105 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index cf755258ac..eb3ec16d01 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -471,6 +471,7 @@ "alt-.": "vim::RepeatFind", "alt-b": "editor::MoveToStartOfLargerSyntaxNode", "alt-e": "editor::MoveToEndOfLargerSyntaxNode", + "shift-g": "vim::HelixGotoLine", // Changes "shift-r": "editor::Paste", diff --git a/crates/vim/src/helix.rs b/crates/vim/src/helix.rs index 99f8ed2fe9..9b2ecf8f5a 100644 --- a/crates/vim/src/helix.rs +++ b/crates/vim/src/helix.rs @@ -42,6 +42,8 @@ actions!( HelixInsertEndOfLine, /// Goes to the location of the last modification. HelixGotoLastModification, + /// Goes to the line specified by the count. + HelixGotoLine, /// Select entire line or multiple lines, extending downwards. HelixSelectLine, /// Select all matches of a given pattern within the current selection. @@ -76,6 +78,7 @@ pub fn register(editor: &mut Editor, cx: &mut Context) { Vim::action(editor, cx, Vim::helix_insert_end_of_line); Vim::action(editor, cx, Vim::helix_yank); Vim::action(editor, cx, Vim::helix_goto_last_modification); + Vim::action(editor, cx, Vim::helix_goto_line); Vim::action(editor, cx, Vim::helix_paste); Vim::action(editor, cx, Vim::helix_select_regex); Vim::action(editor, cx, Vim::helix_keep_newest_selection); @@ -858,6 +861,17 @@ impl Vim { self.jump(".".into(), false, false, window, cx); } + pub fn helix_goto_line( + &mut self, + _: &HelixGotoLine, + window: &mut Window, + cx: &mut Context, + ) { + if let Some(line_number) = Vim::take_count(cx) { + self.helix_move_cursor(Motion::StartOfDocument, Some(line_number), window, cx); + } + } + pub fn helix_select_lines( &mut self, _: &HelixSelectLine, @@ -4431,6 +4445,97 @@ mod test { ); } + #[gpui::test] + async fn test_helix_goto_line(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + cx.enable_helix(); + + // lands at column 0 of the target line + cx.set_state( + indoc! {" + line one + line two + line ˇthree"}, + Mode::HelixNormal, + ); + cx.simulate_keystrokes("2 shift-g"); + cx.assert_state( + indoc! {" + line one + ˇ line two + line three"}, + Mode::HelixNormal, + ); + + // including when targeting the current line + cx.set_state( + indoc! {" + line one + line two + line ˇthree"}, + Mode::HelixNormal, + ); + cx.simulate_keystrokes("3 shift-g"); + cx.assert_state( + indoc! {" + line one + line two + ˇline three"}, + Mode::HelixNormal, + ); + + // but not when count is missing entirely + cx.set_state( + indoc! {" + line one + line two + line ˇthree"}, + Mode::HelixNormal, + ); + cx.simulate_keystrokes("shift-g"); + cx.assert_state( + indoc! {" + line one + line two + line ˇthree"}, + Mode::HelixNormal, + ); + + // bare shift-g does not exit select mode + cx.set_state( + indoc! {" + line one + line two + line ˇthree"}, + Mode::HelixSelect, + ); + cx.simulate_keystrokes("shift-g"); + cx.assert_state( + indoc! {" + line one + line two + line ˇthree"}, + Mode::HelixSelect, + ); + + // shift-g with count jumps without extending the selection, unlike gg + cx.set_state( + indoc! {" + line one + line two + line «ˇthree»"}, + Mode::HelixSelect, + ); + cx.simulate_keystrokes("1 shift-g"); + cx.assert_state( + indoc! {" + ˇline one + line two + line three"}, + Mode::HelixSelect, + ); + } + #[gpui::test] async fn test_helix_end_of_document(cx: &mut gpui::TestAppContext) { let mut cx = VimTestContext::new(cx, true).await;