繁体   English   中英

VIM 可以自动缩进 SQL 吗?

[英]Can VIM autoindent SQL?

SQL Statement indentation good practice ”似乎是编写 SQL 块的公认格式。

是否有符合此标准或至少接近此标准的 Vim 缩进/语法文件?

目前我的 Vim 留下了几乎所有东西,并且只缩进某些关键字。

通过安装 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 --帮助

SQLUtilities : SQL 实用程序 - 格式化、生成 - 列列表、数据库过程”具有 SQL Utilities 插件,该插件功能强大。 而“ How to auto-format and auto-capitalize SQL in Vim ”是一个相关的讨论。

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

您可以使用vim-autoformat插件:

  • 用你最喜欢的插件管理器安装vim-autoformat (我更喜欢轻量级vim-plug
  • 使用pip安装sqlparse
  • 将以下行添加到您的 vim/nvim 配置中
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-client

像 Valerio 的回答一样,我建议将sqlformat albiet 与 Vim 的内置formatprg一起使用,默认情况下是gq

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

我的~/.vim/after/ftplugin/sql.vim文件下有上面的行。

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

暂无
暂无

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

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