21

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

Community
  • 1
  • 1
Mike
  • 5,568
  • 11
  • 31
  • 45

5 Answers5

27

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

petrus-jvrensburg
  • 1,353
  • 15
  • 19
Valerio Crini
  • 1,829
  • 3
  • 13
  • 14
  • I mapped \f to call _sqlformat_ by adding this one line to my _~/.vimrc_: ```map \f :%!sqlformat --reindent --indent_columns --keywords upper --identifiers lower -``` – petrus-jvrensburg Jan 04 '22 at 13:26
  • Mind that `autocmd FileType sql call SqlFormatter()` will make this work only in a sql file. If you want it to be called at the start of any file so that the shortcut is always available, use `nnoremap call SqlFormatter()` instead. This might be interesting if you have SQL strings embedded in another language. Since sqlformat works on a whole file, you need to use another formatter, see [Is there a way to let sqlformat change only a selection within a file in VIM?](https://vi.stackexchange.com/questions/36643/is-there-a-way-to-let-sqlformat-or-another-sql-formatter-change-only-a-selection). – questionto42 Jan 28 '22 at 11:13
11

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
8

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

Dzamo Norton
  • 1,194
  • 11
  • 17
6

You can use the vim-autoformat plugin:

  • Install vim-autoformat with your favourite plugin-manager (I prefer lightweight vim-plug)
  • Install sqlparse with pip
  • Add the following lines to your vim/nvim config
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 should rebuild your vim with python support or install python-client for neovim

lucidyan
  • 3,575
  • 2
  • 22
  • 24
3

Like Valerio's answer, I recommend using sqlformat albiet with Vim's builtin formatprg, which is gq by default.

setlocal formatprg=sqlformat\ --reindent\ --keywords\ upper\ --identifiers\ lower\ -

I have the line above under my ~/.vim/after/ftplugin/sql.vim file.

This allows you to use Vim's builtin gq against any selection, or against known Vim's objects within sql, or simply gggqG to reindent the entire buffer.

Meitham
  • 9,178
  • 5
  • 34
  • 45