tldr; How do pass stdout of BufWritePost to an external command?

On command line I can run

cat index.html | tidy -indent -modify

I'm trying to call tidy automatically when I :w

This is what I have so far:

:function! Tidy()
:  call tidy -indent -modify --doctype omit --show-body-only yes 

autocmd BufWritePost *.html call Tidy()

I'm missing some vimscript skills

  • Probably you just need to use a filter (:help filter should get you there; something like :%!tidy …). Otherwise there are system/sytemlist. You might want BufWritePre if you change the contents.
    – D. Ben Knoble
    Apr 28 at 21:26
  • Thanks, what is the meaning of % in this context? Yes BufWritePre makes more sense
    – user885983
    Apr 28 at 21:39
  • Try :help [range]; it means filter (!) the whole buffer (%)
    – D. Ben Knoble
    Apr 28 at 21:52

Assuming the intent is to replace the contents with the tidy’d contents before writing, I would use BufWritePre and a filter like

:%!tidy …

This gives the buffer as standard in and replaces it with standard out.

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.