How do I create a new buffer as a clone of another buffer? In other words, the contents should be the same, but these should be 2 distinct buffers in the end. I'm more interested in programmatic way of doing this, rather than from the point of view of mappings, as this is needed for the future plugin.
Recommendations like yanking one buffer, then giving focus to another one and pasting wouldn't work here since it would mean that we've overwritten user's previous yank and therefore spoiled user's experience after the invocation of our plugin method. I'm looking for some way to do it as sneaky as possible.
Furthermore, as a bonus, this new buffer should be created hidden because it will be used just to store some processed data quickly and then die immediately. Preferably, this should be done without any splits as they will introduce annoying blinking (because of Vim redrawing the screen) even if closed as soon as possible (for example, :new
/:close
combo). We also cannot assume that the buffer which we want to clone has a name.
Thank you.
Conclusion
Some testing revealed that transferring data with
:put=getbufline(...)
is the fastest method, and the most secure one since it does not interfere with user experience in any way.
Although, nobody answered on this, I've managed to find a function which will create a new hidden buffer without any splits, i.e. it will be created silently on the background with no window. To learn more see :h bufnr()
. Example:
let buffer_number = bufnr('My New Hidden Buffer', 1)
Furthermore, this call will implicitly do the following:
let &l:buflisted = 0
on this newly created buffer, so it will be also hidden from :ls
, which is exactly what we need to satisfy above requirements.
As a result, there will be no blinking or any visual irritation for the user like from :new
/:close
combo... a truly hidden buffer for some dirty work.