Skip to content

vi: visual-block delete then paste only reinserts the last row #1

Closed

Description

@ernestrc

In vi mode, selecting a multi-line visual block with <c-v>, deleting it with d or x, and then pasting with p or P only reinserts the last row of the block. Vim pastes a block vertically: row N of the yanked block goes to row N below the cursor.

Yank-then-paste of a block already works. Only the delete-then-paste path is broken.

Steps to reproduce

  1. Open a file with several lines in vi mode.
  2. Press <c-v>, extend the selection down a few rows and right a few columns.
  3. Press d.
  4. Move the cursor elsewhere and press p.

Actual: a single line is inserted, containing only the last row of the deleted block.
Expected: the whole block is reinserted vertically, one segment per line.

Root cause

Two writers touch the unnamed register during a visual-block delete. The vi handler first copies the full block (rows joined with \n, metadata BlockSelection), which is correct. The delete then runs one edit per row, and each row's edit notifies the delete-clipboard subscriber, which overwrites the register with just that row's text. After the loop the register holds only the last row, so paste inserts a single line.

Where to look

  • internal/text/delete_clipboard.go - the OnDidEdit subscriber that overwrites the register per row.
  • internal/text/vi/handler.go - copySelectionForDelete and the visual-mode d/x handler.
  • internal/cell/buffer.go - DeleteBlockContext, the per-row edit loop.
  • internal/text/cursor.go - Cursor.Paste / InsertBlock, which already handle blockwise pastes correctly when the register holds the joined text.

Notes for contributors

Per the bug-fix policy, start with a failing regression test. Table-driven cases worth covering: d then p, x then p, d then P, and a ragged block where rows have unequal length. The existing blockwise tests in internal/text/vi/handler_test.go are a good template. Make sure the black-hole register ("_d) behavior does not regress.

Activity

  1. added
    bug
    help wanted
    on Sep 11, 2026
  2. ikshantshukla123 commented on Sep 12, 2026

    @ikshantshukla123
    Contributor

    I'd like to take this. I confirmed the root cause: copySelectionForDelete (internal/text/vi/handler.go) writes the full block correctly, then DeleteSelection → DeleteBlockContext (internal/cell/buffer.go) fires one OnDidEdit per row and delClip (internal/text/delete_clipboard.go) overwrites the register each time, leaving only the last row.

    Planned fix: in the visual-block d/x path, save the register data from copySelectionForDelete and re-assert it after DeleteSelection() (scoped to BlockSelection only, so char/line behavior is untouched). This also fixes "_d on blocks touching unnamed via delClip.

    Regression tests (per bug-fix policy) at Vi integration level with WithCopyDelete installed handler-level tests bypass it covering d→p, x→p, d→P, a ragged block, and black-hole preservation.

    Noted adjacent instances of the same pattern (c/s on block, visual-paste-replace, dot-repeat) I'll keep this PR to d/x unless you'd prefer them bundled. Let me know if the approach sounds good.

  3. added a commit that references this issue on Sep 15, 2026
    e34d2bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions