I have content stored in a variable (out
) which I want to replace with the current buffer. I'm 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)
(Detailed: https://github.com/fatih/vim-go/blob/master/autoload/go/fmt.vim#L130)
However setline()
here causes slowness on some machines and https://github.com/fatih/vim-go/issues/459. I've profilde it myself but for me setline was not a problem. Anyway, I need a solution which is more faster. So I've come up with several other solutions.
First one is, which puts the output to a register, deletes all lines and then puts it back:
let @a = out
% delete _
put! a
$ delete _
Second solution would be using append()
(which was used previously 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 they both also causes a side effect which I'm still couldn't solve and is also written in the title. The problem is described as:
If a buffer is opened in another view (say next to next), and we call one of the two solutions above, it breaks the cursor of the other view and jumps to the bottom
Here is a GIF showing it better (whenever I call :w
one of the procedures above is called): http://d.pr/i/1buDZ
Is there a way, to replace the content of a buffer, which is fast and doesn't break the layout? Or how can I prevent it with one of the procedures above?
Thanks.