diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 00556e182d82..ae90a92b0275 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -424,18 +424,33 @@ pub fn extend_prev_char(cx: &mut Context) { pub fn replace(cx: &mut Context) { // need to wait for next key cx.on_next_key(move |cx, event| { - if let KeyEvent { - code: KeyCode::Char(ch), - .. - } = event - { - let text = Tendril::from_char(ch); + let ch = match event { + KeyEvent { + code: KeyCode::Char(ch), + .. + } => Some(ch), + KeyEvent { + code: KeyCode::Enter, + .. + } => Some('\n'), + _ => None, + }; + if let Some(ch) = ch { let (view, doc) = cx.current(); let transaction = Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { - (range.from(), range.to() + 1, Some(text.clone())) + let max_to = doc.text().len_chars().saturating_sub(1); + let to = std::cmp::min(max_to, range.to() + 1); + let text: String = doc + .text() + .slice(range.from()..to) + .chars() + .map(|c| if c == '\n' { '\n' } else { ch }) + .collect(); + + (range.from(), to, Some(text.into())) }); doc.apply(&transaction, view.id);