diff --git a/src/reader.rs b/src/reader.rs index a63bf578c..27a603ab1 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -4067,8 +4067,11 @@ fn clear_pager(&mut self) { fn get_selection(&self) -> Option> { let selection = self.selection?; - let start = selection.start; + let start = std::cmp::min(selection.start, self.command_line.len()); let end = std::cmp::min(selection.stop, self.command_line.len()); + if start == end { + return None; + } Some(start..end) } diff --git a/tests/pexpects/bind.py b/tests/pexpects/bind.py index 74af4af35..fe287f573 100644 --- a/tests/pexpects/bind.py +++ b/tests/pexpects/bind.py @@ -30,6 +30,21 @@ expect_prompt(increment=False) send("\f") expect_prompt(increment=False) +# Test that kill-selection after selection is cleared doesn't crash +sendline("bind ctrl-space begin-selection") +expect_prompt() +sendline("bind ctrl-w kill-selection end-selection") +expect_prompt() +send("echo 123") +# Send Ctrl-Space using CSI u encoding +send("\x1b[32;5u") +# Send Ctrl-C to clear the command line +send("\x1b[99;5u") +# Send Ctrl-W which used to crash +send("\x1b[119;5u") +sendline("bind --erase ctrl-space ctrl-w") +expect_prompt() + # Fish should start in default-mode (i.e., emacs) bindings. The default escape # timeout is 30ms. #