通过安装Python模块sqlparse,可以在终端中使用sqlformat
命令。
pip install sqlparse
从vim中,您可以使用以下命令:
:%!sqlformat --reindent --keywords upper --identifiers lower -
为了添加一个快捷方式,pt,我在我的.vimrc
配置文件中添加了以下配置:
autocmd FileType sql call SqlFormatter()
augroup end
function SqlFormatter()
set noai
" set mappings...
map ,pt :%!sqlformat --reindent --keywords upper --identifiers lower -<CR>
endfunction
您可以稍微定制sqlformat。请参见
sqlformat --help
"SQLUtilities : SQL工具 - 格式化、生成列列表、数据库过程" 有SQL Utilities插件,能够实现功能。而 "如何在Vim中自动格式化和自动大写SQL" 是一个相关的讨论。
:CocCommand
可以带你进入一个模糊查找器 - 输入 sql.Format
就可以了。 - Kevin Friedheimvim-autoformat
插件。pip
安装 sqlparse
.noremap <F3> :Autoformat<CR>
let g:formatdef_sql = '"sqlformat --reindent --keywords upper - identifiers lower -"'
let g:formatters_sql = ['sql']
vim has no support for python
,那么你应该重新构建支持python的vim或者安装neovim的python客户端。和Valerio的回答一样,我建议使用sqlformat,不过要用Vim内置的formatprg
,默认是gq
。
setlocal formatprg=sqlformat\ --reindent\ --keywords\ upper\ --identifiers\ lower\ -
~/.vim/after/ftplugin/sql.vim
文件中添加了上述代码。gq
命令来处理任何选择,或者针对sql中已知的Vim对象,或者只需使用gggqG
重新缩进整个缓冲区。
map \f :%!sqlformat --reindent --indent_columns --keywords upper --identifiers lower -<CR>
- petrus-jvrensburgautocmd FileType sql call SqlFormatter()
只会在 SQL 文件中起作用。如果您希望在任何文件的开头调用它,以便始终可用快捷方式,请改用nnoremap call SqlFormatter()
。如果您在另一种语言中嵌入了 SQL 字符串,则可能会很有趣。由于 sqlformat 适用于整个文件,因此您需要使用另一个格式化程序,请参见 Is there a way to let sqlformat change only a selection within a file in VIM?。 - questionto42