If your Vim version is more recent than 8.0.1039, you could use the
setbufline() function. It allows you to replace a line in an arbitrary buffer with any text.
call setbufline(12, 34, 'hello world')
This should replace the text on the line
34 inside the buffer
What if the old content has more lines than the new content?
Do you know how I would get rid of those lines?
Maybe you could replace the first lines of the buffer with the text you have stored in your variable, then replace all the rest with empty strings:
let your_buffer_number = 123
let your_new_lines = ['foo', 'bar', 'baz']
let remaining_lines = len(getbufline(your_buffer_number, 1, '$')) - len(your_new_lines)
call setbufline(your_buffer_number, 1, your_new_lines + repeat([''], remaining_lines))
This will not delete the superfluous lines, but it should make them empty.
Replace the buffer number
123 with the one you're actually using, and the list of texts
['foo', 'bar', 'baz'] with the ones you want to write in your buffer.
For more information, see:
If you're using Neovim, and it's more recent than v0.2.0, then you could use the
call nvim_buf_set_lines(123, 0, len(getbufline(123, 1, '$')), 0, ['foo', 'bar', 'baz'])
This should replace all the lines in the
123th buffer with the lines
As a result, this function would also delete the superfluous lines, if the old contents of the buffer is longer than the new one.
Note that I'm not sure about the exact Neovim version which introduced the function, but
0.2.0 is the one given by