diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 4df3278b8..d44f477b7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3651,7 +3651,7 @@ pub fn insert_newline(cx: &mut Context) { (pos, pos, local_offs) }; - let new_range = if doc.restore_cursor { + let new_range = if range.cursor(text) > range.anchor { // when appending, extend the range by local_offs Range::new( range.anchor + global_offs, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index db0d4030b..0fa6d67c9 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1990,10 +1990,12 @@ pub fn enter_normal_mode(&mut self) { if doc.restore_cursor { let text = doc.text().slice(..); let selection = doc.selection(view.id).clone().transform(|range| { - Range::new( - range.from(), - graphemes::prev_grapheme_boundary(text, range.to()), - ) + let mut head = range.to(); + if range.head > range.anchor { + head = graphemes::prev_grapheme_boundary(text, head); + } + + Range::new(range.from(), head) }); doc.set_selection(view.id, selection);