0
func! ShouldReformat()
  return normal :w !elm-format --stdin --validate
endfunc

func! Reformat()
  if ShouldReformat()
    normal gggqG
  endif
endfunc

But return normal is not taken into account, how can I test that ?

1

1 Answer 1

1
func! ShouldReformat()
  write !elm-format --stdin --validate
  return v:shell_error
endfunc

v:shell_error returns the last shell execution exit code

:help shell_error
v:shell_error   Result of the last shell command.  When non-zero, the last
                shell command had an error.  When zero, there was no problem.
                This only works when the shell returns the error code to Vim.
                The value -1 is often used when the command could not be
                executed.  Read-only.
3
  • 2
    You dont need normal here; the bodies of functions are Ex commands. So just write !elm-format … is fine.
    – D. Ben Knoble
    Commented Jan 30, 2022 at 15:02
  • 1
    Then elm had an output, so I guess i'll swap for system() Commented Jan 30, 2022 at 15:53
  • 1
    Ok, my error was dumb : I used system(..., join(getline(1, '$'), "\n")), and was missing the closing "\n" Commented Jan 30, 2022 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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