Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can VIM autoindent SQL?

"SQL Statement indentation good practice" appears to be the accepted format for writing SQL blocks.

Is there a Vim indent/syntax file that would adhere to this standard, or at least be close?

Currently my Vim left alights pretty much everything and only indents certain keywords.

like image 820
Mike Avatar asked Dec 20 '11 15:12

Mike


3 Answers

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

like image 139
Valerio Crini Avatar answered Nov 20 '22 21:11

Valerio Crini


"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.

like image 31
Zsolt Botykai Avatar answered Nov 20 '22 20:11

Zsolt Botykai


If you use coc.nvim then you can add the coc-sql extension.

like image 8
Dzamo Norton Avatar answered Nov 20 '22 19:11

Dzamo Norton