Skip to content

fix(vi): move cursor left when exiting insert mode with Escape#1065

Draft
sim590 wants to merge 2 commits intonushell:mainfrom
sim590:fix-vi-esc-cursor
Draft

fix(vi): move cursor left when exiting insert mode with Escape#1065
sim590 wants to merge 2 commits intonushell:mainfrom
sim590:fix-vi-esc-cursor

Conversation

@sim590
Copy link
Copy Markdown

@sim590 sim590 commented May 4, 2026

Summary

  • Move the cursor one position left when pressing Escape to exit insert mode, matching standard Vi behavior (insert mode cursor is between characters, normal mode cursor is on a character)
  • Adjust is_cursor_at_buffer_end() to treat "cursor on the last character" as "at buffer end" in Vi normal mode, preserving prefix history search

Context

This fix was previously implemented in #699 but reverted in #773 because it broke prefix history search (#772): after pressing Escape, the cursor moved left, causing is_cursor_at_buffer_end() to return false, which disabled prefix search when pressing k.

This PR addresses that regression by making is_cursor_at_buffer_end() mode-aware. In Vi normal mode, the cursor sitting on the last character is equivalent to being at the end of the buffer.

A similar fix was proposed in #986 but did not address the prefix search regression.

Closes #694 (pt 1)

Test plan

  • Added esc_from_insert_mode_moves_cursor_left test
  • Added esc_from_normal_mode_does_not_move_cursor test
  • Added test_is_cursor_at_buffer_end_vi_normal_mode test
  • Added test_is_cursor_at_buffer_end_insert_mode test
  • All 843 tests pass, clippy clean

sim590 added 2 commits May 4, 2026 02:02
In Vi, insert mode places the cursor between characters while normal
mode places it on a character. Pressing Escape to leave insert mode
must move the cursor one position left to match standard Vi behavior.

When Escape is pressed from normal mode, the cursor is not moved.

Closes nushell#694 (pt 1)
After the Escape cursor fix, the cursor in Vi normal mode sits on the
last character rather than after it. This caused is_cursor_at_buffer_end()
to return false, breaking prefix history search (regression seen in nushell#772
which led to the revert of nushell#699).

Treat "cursor on the last character" as equivalent to "at buffer end"
when the editor is in Vi normal mode, so that prefix history search
still triggers correctly after pressing Escape.
@fdncred fdncred marked this pull request as draft May 4, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vi mode cursor position isn't vi-like

1 participant