1

For example, if I run some external program (formatter) from vim and write result of it to some variable:

let result=system("some_program", getline(1, "$"))
" do something with result

How I can replace all current buffer content by the result?

PS result must be same as %!some_program, but I can not use this. If program failed it return not 0 code and error message with line and column of error, I use this message for create cbuffer. So I need write result of program to variable, check return code and handle it.

3
  • PS result must be same as %!some_program. Why not just use %!some_program then? Update your question to indicate why using that is not an option... – filbranden Jan 28 '20 at 14:31
  • 1
    @filbranden update question – user20084 Jan 28 '20 at 14:36
  • Thanks for the update! – filbranden Jan 28 '20 at 14:38
2

You can try this:

silent let result=systemlist("some_program", getline(1, "$"))
" do something with result
" cleanup buffer
normal! gg_dG
call setline(1, result)

Or this:

silent let result=system("some_program", getline(1, "$"))
" do something with result
" cleanup buffer
:%delete _
call setline(1, split(result, '\n'))

If you want specific buffer to be replaced, use setbufline and adjust "cleanup buffer" command.

3
  • 2
    Your delete command can be written as :%delete _, though I'm not sure it's necessary with setline – D. Ben Knoble Jan 28 '20 at 14:23
  • 3
    There's also deletebufline(), so you could use call deletebufline('.', 1, '$') to delete everything. @D.BenKnoble just using setline() directly won't work if result has fewer lines than the original buffer contents. – filbranden Jan 28 '20 at 14:42
  • @D.BenKnoble, indeed :%delete _ is better here. Thx! – Maxim Kim Jan 29 '20 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.