Skip to content

buffer-updates: on_bytes emits before buffer is modified when doing search-and-replace #36370

@noib3

Description

@noib3

Problem

When editing a buffer via search-and-replace, the on_bytes callback given to nvim_buf_attach seems to be called with the old buffer contents, making it impossible to get the new text.

Steps to reproduce

  • create this minimal.lua:
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "Hello" })

local buffer_lines = {}

vim.api.nvim_buf_attach(0, false, {
  on_bytes = function()
    buffer_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
  end
})

vim.cmd("s/llo/y")

-- 💥 buffer_lines is { "Hello" }.
assert(
  vim.deep_equal(buffer_lines, { "Hey" }),
  ("buffer_lines: %s"):format(vim.inspect(buffer_lines))
)
  • run nvim --clean -u minimal.lua

Expected behavior

When called in on_bytes, vim.api.nvim_buf_get_{text|lines} returns the new contents .

Nvim version (nvim -v)

NVIM v0.12.0-nightly+a04c73c

Vim (not Nvim) behaves the same?

N/A

Operating system/version

macOS 15.7

Terminal name/version

Ghostty

$TERM environment variable

ghostty

Installation

Nix flake

Metadata

Metadata

Assignees

No one assigned

    Labels

    apilibnvim, Nvim RPC APIhas:reproissue contains minimal reproducing steps

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions