Skip to content

editor: Fix clipboard selection range for multi-line copy-and-trim#48977

Merged
dinocosta merged 7 commits intomainfrom
fix-issue-46463-0287136e79e9731c
Feb 24, 2026
Merged

editor: Fix clipboard selection range for multi-line copy-and-trim#48977
dinocosta merged 7 commits intomainfrom
fix-issue-46463-0287136e79e9731c

Conversation

@yeskunall
Copy link
Member

@yeskunall yeskunall commented Feb 11, 2026

When copying multiple selections with copy-and-trim, create a single clipboard selection spanning the original buffer range rather than one selection per trimmed line. This preserves correct paste behavior in Vim mode when pasting trimmed content.

Closes #48869.

  • Tests or screenshots needed?
  • Code Reviewed
  • Manual QA

Release Notes:

  • Fixed clipboard selection range for multi-line copy-and-trim

When copying multiple selections with copy-and-trim, create a single
clipboard selection spanning the original buffer range rather than
one selection per trimmed line. This preserves correct paste behavior
in vim mode when pasting trimmed content.
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Feb 11, 2026
@zed-community-bot zed-community-bot bot added the staff Pull requests authored by a current member of Zed staff label Feb 11, 2026
@dinocosta dinocosta self-assigned this Feb 12, 2026
Copy link
Member

@dinocosta dinocosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you tackling this one @yeskunall ! 🙌

There's a couple of points I'd like to explore before merging this:

  1. Removing the test from the vim crate, as the fix is actually on the editor side of things, so we can easily assert that a single ClipboardSelection is created
  2. Refactoring a bit the do_copy method as it's already quite big and these changes add more complexity to it

Let me know if you want me to take a quick look at these or if you'd like to pair 🙂

dinocosta and others added 2 commits February 23, 2026 16:03
Co-authored-by: Kunall Banerjee <hey@kimchiii.space>
* Avoid multiple `clipboard_selections.push`, moving it to out of the
  inner loop.
* Remove unnecessary block `{ ... }` before iterating over selections,
  reducing the indentation level.
* Assign to `prev_selection_was_entire_line` once as the value never
  changed inside the inner loop.

Co-authored-by: Kunall Banerjee <hey@kimchiii.space>
@dinocosta
Copy link
Member

@yeskunall I just noticed that it seems the behavior reported in #48869 has changed in Nightly, it's still not working as it should but it's now preserving the newlines, so I do wonder if #49134 also affected this behavior ▼

CleanShot.2026-02-23.at.17.44.22.mp4

Will take a closer look tomorrow to ensure we actually ship the correct behavior to Preview on Wednesday ✅

@yeskunall
Copy link
Member Author

@dinocosta interesting -- if I get the chance today I’ll take a look as well. Ping me tomorrow again if you think we need to pair!

In `main`, the `editor: copy and trim` behavior is currently broken when
pasting with vim's `p`, as the last character is ignored. For example,
when copying the `values` field from this JSON object:

```
{
data: {
values: {
high: 1,
mid: 0.5,
low: 0
}
}
}
```

The pasted contents, using `editor: copy and trim`, would end up being:

```
values: {
high: 1,
mid: 0.5,
low: 0
```

Where the closing `}` is not pasted. The changes in this branch fix this
but we were missing a test to actually prevent a regression.
Move the test that was introduced in vim's crated, namely to assert that
copy and trim, then pasting, would work as expected, to a test in the
editor crate since the fix was done in that crate and we can assert the
fix by ensuring that a multiline selection ends up creating a single
`ClipboardSelection`.
@dinocosta
Copy link
Member

@dinocosta interesting -- if I get the chance today I’ll take a look as well. Ping me tomorrow again if you think we need to pair!

@yeskunall I've confirmed that the changes in this branch fix the issue with editor: copy and trim followed by pasting in vim mode that currently exists in main, all good there! 🙌

Also pushed two commits, one adding a test case for catching a regression that bug, as well as moving the test that was added in this PR to the editor crate, simply asserting that a single ClipboardSelection was created, as we discussed yesterday. Will merge, thanks!

@dinocosta dinocosta enabled auto-merge (squash) February 24, 2026 17:08
@dinocosta dinocosta merged commit c5aea77 into main Feb 24, 2026
27 checks passed
@dinocosta dinocosta deleted the fix-issue-46463-0287136e79e9731c branch February 24, 2026 17:14
@yeskunall
Copy link
Member Author

Thanks @dinocosta! Really appreciate you seeing this all the way through.

tahayvr pushed a commit to tahayvr/zed that referenced this pull request Mar 4, 2026
…ed-industries#48977)

When copying multiple selections with copy-and-trim, create a single
clipboard selection spanning the original buffer range rather than one
selection per trimmed line. This preserves correct paste behavior in Vim
mode when pasting trimmed content.

Closes zed-industries#48869.

Release Notes:

- Fixed clipboard selection range for multi-line copy-and-trim

---------

Co-authored-by: dino <dinojoaocosta@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy and trim collapses newlines in vim mode

2 participants