Format SQL in Vim

When I save a .sql file in Vim, it auto-formats it with pgformatter using the flags from my ftplugin/sql.vim.

Install and configure ALE in ~/.vimrc:

call plug#begin('~/.vim/plugged')
  Plug 'dense-analysis/ale' " :help ale
call plug#end()

In ~/.vim/ftplugin/sql.vim:

" Auto-fix
let b:ale_fixers = ['pgformatter']
let g:ale_fix_on_save = 1
let b:ale_sql_pgformatter_options = '--function-case 1 --keyword-case 2 --spaces 2 --no-extra-line'

In my, I have a variant of the following that is idempotent; it will install or update Homebrew and pgFormatter:

# pgformatter
brew install pgformatter

# Vim plugins
curl -fLo "$HOME/.vim/autoload/plug.vim" --create-dirs \
vim -u "$HOME/.vimrc" +PlugUpdate +PlugClean! +qa