Hi, I was wondering if black.vim is able to integrate with vim's 'formatprg' option (:h 'formatprg'), which is supposed to be set to an external command to run when using 'gq' operator. I'm not sure if it's possible or at-odds with how black is supposed to be run, can anyone clarify? Docs say the external program must take the input on stdin and produce output on stdout.
Best effort so far:
in my personal ~/.vim/after/ftplugin/python.vim:
setlocal formatprg=black\ --quiet\ -
augroup black-fmt
autocmd!
autocmd BufWritePre <buffer> normal gggqG``
augroup END
This will set a local autocommand to run black on the current buffer file each time it is about to
save, but only if :set ft? value is 'python'. 'gq' operator works with stdin and stdout, not stderr so we tell black to be --quiet (else you get messages from black pasted into your file) and we pass it the '-' argument to tell black to use stdin.
I'm sure there are improvements that can be made, open to suggestions.
Above has one disadvantage: loses cursor position (sends you to top of file).
@mooreniemi This kinda works, but since the file is changed outside of Vim it cannot track the changes to a mark (cannot remember "current" position).
autocmd BufWritePre <buffer> keepjumps normal m'gggqG``
Most helpful comment
Best effort so far:
in my personal ~/.vim/after/ftplugin/python.vim:
This will set a local autocommand to run black on the current buffer file each time it is about to
save, but only if :set ft? value is 'python'. 'gq' operator works with stdin and stdout, not stderr so we tell black to be --quiet (else you get messages from black pasted into your file) and we pass it the '-' argument to tell black to use stdin.
I'm sure there are improvements that can be made, open to suggestions.