I have content stored in a variable ( out ) that I want to replace with the current buffer. I am currently doing it like this (simplified version):
let splitted = split(out, '\n') if line('$') > len(splitted) execute len(splitted) .',$delete' endif call setline(1, splitted)
(Details: https://github.com/fatih/vim-go/blob/master/autoload/go/fmt.vim#L130 )
However, setline() here causes a slowdown on some machines and https://github.com/fatih/vim-go/issues/459 . I myself profiled it, but for me setting was not a problem. Anyway, I need a solution that is faster. Therefore, I came up with several other solutions.
The first, which places the output in a register, deletes all lines and then returns it:
let @a = out % delete _ put! a $ delete _
The second solution will use append() (previously used in vim-go https://github.com/fatih/vim-go/commit/99a1732e40e3f064300d544eebd4153dbc3c60c7 ):
let splitted = split(out, '\n') %delete _ call append(0, splitted) $delete _
They both work! However, both of them also cause a side effect, which I still canβt solve, and it is also written in the title. The problem is described as:
If the buffer is opened in another view (say, next), and we call one of the two solutions listed above, it breaks the cursor into another view and jumps to the bottom
Here is a GIF showing it better (when I call :w one of the above procedures is called): http://d.pr/i/1buDZ
Is there a way to replace the contents of a buffer that is fast and does not break the layout? Or how can I prevent this with one of the above procedures?
Thanks.