Read From A Neovim Buffer

June 2021

This is the basic way to read lines from a Neovim buffer/file.

local read_lines = function()

    local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

    for k, line in ipairs(lines) do
        vim.api.nvim_buf_set_lines(
            new_floating_buffer, k - 1, -1, true, { line }
        )
    end

end

Notes

  • Arguments are: buffer number, start line, end line, and strict indexing

Get The Last Line

local last_line = vim.api.nvim_buf_get_lines(buffer_number, -2, -1, false)
end of line