简体   繁体   中英

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.

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

如果你使用coc.nvim那么你可以添加coc-sql 扩展

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM