While there are packages that format on save, I'd like to be able to run a custom command that auto-formats a file on save.
Upon saving it runs an external process that:
- Takes the buffer as
- Outputs to a temporary file.
The output is used to replace the current buffer which is then saved.
Any errors from the
stderrare reported as errors.
- Any output from the
stdoutis printed as messages.
For the purpose of testing, this could be the auto-formatting command:
It converts the text to title-caps.
python -c "with open(__import__('sys').argv[-1], 'w') as fh: fh.write(__import__('sys').stdin.read().title())" -- /tmp/TEMP_FILE_FROM_EMACS.txt
/tmp/TEMP_FILE_FROM_EMACS.txt is a generated temp file name.
I realize this may be an involved answer. If this seems like too much hassle to answer, I'll investigate and post an answer myself, since I think it's useful to have a general function to handle this.