简体   繁体   English

VIM 可以自动缩进 SQL 吗?

[英]Can VIM autoindent SQL?

" SQL Statement indentation good practice " appears to be the accepted format for writing SQL blocks. SQL Statement indentation good practice ”似乎是编写 SQL 块的公认格式。

Is there a Vim indent/syntax file that would adhere to this standard, or at least be close?是否有符合此标准或至少接近此标准的 Vim 缩进/语法文件?

Currently my Vim left alights pretty much everything and only indents certain keywords.目前我的 Vim 留下了几乎所有东西,并且只缩进某些关键字。

By installing the python module sqlparse , which makes the sqlformat command available in your terminal.通过安装 python 模块sqlparse ,这使得sqlformat命令在您的终端中可用。

pip install sqlparse

from vim you can use从 vim 你可以使用

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

in order to attach a shortcut ,pt I added following configuration to my .vimrc config file:为了附加快捷方式,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

You can customize sqlformat a bit.您可以稍微自定义 sqlformat。 See

sqlformat --help sqlformat --帮助

" SQLUtilities : SQL utilities - Formatting, generate - columns lists, procedures for databases " has the SQL Utilities plugin, which is capable. SQLUtilities : SQL 实用程序 - 格式化、生成 - 列列表、数据库过程”具有 SQL Utilities 插件,该插件功能强大。 And " How to auto-format and auto-capitalize SQL in Vim " is a related discussion.而“ How to auto-format and auto-capitalize SQL in Vim ”是一个相关的讨论。

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

You can use the vim-autoformat plugin:您可以使用vim-autoformat插件:

  • Install vim-autoformat with your favourite plugin-manager (I prefer lightweight vim-plug )用你最喜欢的插件管理器安装vim-autoformat (我更喜欢轻量级vim-plug
  • Install sqlparse with pip使用pip安装sqlparse
  • Add the following lines to your vim/nvim config将以下行添加到您的 vim/nvim 配置中
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如果您看到此消息: vim has no support for python ,您应该使用 python 支持重建您的 vim 或为 neovim 安装python-client

Like Valerio's answer, I recommend using sqlformat albiet with Vim's builtin formatprg , which is gq by default.像 Valerio 的回答一样,我建议将sqlformat albiet 与 Vim 的内置formatprg一起使用,默认情况下是gq

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

I have the line above under my ~/.vim/after/ftplugin/sql.vim file.我的~/.vim/after/ftplugin/sql.vim文件下有上面的行。

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.这允许你使用 Vim 的内置gq来处理任何选择,或者 sql 中已知的 Vim 对象,或者简单地gggqG来重新缩进整个缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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