Can VIM auto-identify SQL?

" Good SQL Indentation Practice " seems to be the accepted format for writing SQL blocks.

Is there a Vim indent / syntax file that conforms to this standard or at least is close?

Currently, my Vim leaves a lot of things and just the indentation of some keywords.

+10
source share
4 answers
+10
source

By installing python sqlparse module

pip install sqlparse

from vim you can use

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

so as not to attach the shortcut , pt , I added the following configuration to the .vimrc configuration 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 tweak sqlformat a bit. Cm.

sqlformat --help

+12
source

You can use vim-autoformat plugin :

  • Install vim-autoformat with your favorite manager plugin (I prefer the lightweight vim-plug )
  • Install sqlparse using pip
  • Add the following lines to the vim / nvim configuration
 noremap <F3> :Autoformat<CR> let g:formatdef_sql = '"sqlformat --reindent --keywords upper - identifiers lower -"' let g:formatters_sql = ['sql'] 

If you see this message: vim has no support for python , you must rebuild your vim with python support or install python-client for neovim

+4
source

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

0
source

All Articles