From 6f480d1d858faac1d285d1bb659b88ab41430163 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Wed, 15 Jan 2025 07:19:50 +0100 Subject: [PATCH] Also trim trailing newlines when adding to history When pasting and executing a full line, the trailing newline character will be included in history. I usually manually delete the newline before executing, but sometimes I forget. When I recall my (typically single-line) commands, it's surprising that the cursor is on the blank second line. The newline doesn't seem useful. Let's remove it automagically. I wonder if anyone will be thrown off by this smart behavior. In future, we can make this space trimming configurable, similar to fish_should_add_to_history. --- src/reader.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index adf1d2537..f4eba3312 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -5722,7 +5722,10 @@ impl<'a> Reader<'a> { // Historical behavior is to trim trailing spaces, unless escape (#7661). let mut text = self.command_line.text().to_owned(); - while text.chars().next_back() == Some(' ') + while text + .chars() + .next_back() + .is_some_and(|c| matches!(c, ' ' | '\n')) && count_preceding_backslashes(&text, text.len() - 1) % 2 == 0 { text.pop();