Can VIM autoindent SQL?

By installing the python module sqlparse, which makes the sqlformat command available in your terminal.

pip install sqlparse

from vim you can use

:%!sqlformat --reindent --keywords upper --identifiers lower -

in order to attach a shortcut ,pt I added following configuration to my .vimrc config file:

autocmd FileType sql call SqlFormatter()
augroup end
function SqlFormatter()
    set noai
    " set mappings...
    map ,pt  :%!sqlformat --reindent --keywords upper --identifiers lower -<CR>
endfunction

You can customize sqlformat a bit. See

sqlformat --help


"SQLUtilities : SQL utilities - Formatting, generate - columns lists, procedures for databases" has the SQL Utilities plugin, which is capable. And "How to auto-format and auto-capitalize SQL in Vim" is a related discussion.