问题描述
我在vim中启用了autoindent和smartindent,但nocindent已关闭.我正在尝试缩进一些JSON文本而没有完整地打印整个内容,这太麻烦了:
I have autoindent and smartindent in vim turned on, but nocindent turned off. I'm trying to indent some JSON text without pretty-printing the whole thing, which would be too intrusive:
{
"a" : "value1",
"b": "value2",
"c": "value3",
...
通过gg = G格式化为:
Gets formatted by gg=G as:
{
"a" : "value1",
"b": "value2",
"c": "value3",
...
那里发生的逻辑是什么,如果可能的话,我可以设置哪些选项来修复它?我尝试切换诸如autoindent,smartindent和cindent之类的选项(带有"no"对应项),但是它对=命令没有影响.我最近的尝试有以下选择:
What's the logic going on there, and what options can I set to fix it, if it's possible? I tried toggling options like autoindent, smartindent, and cindent (with their "no" counterparts), but it doesn't have an effect on the = command. My latest attempt had these options:
autoindent
smartindent
nocindent
cinoptions=
indentexpr=
indentkeys=0{,0},:,0#,!^F,o,O,e
推荐答案
我可以解释其逻辑,但是我不确定是否可以轻松解决. Vim的内部缩进器遵循C风格的语法,因此,由于"a" : "value1",
行不是以;
结尾,因此它假定以下行是该语句的延续,因此应该对其进行缩进以显示该行.
I can explain the logic, but I'm not sure of an easy fix. Vim's internal indenter is following C-style syntax, so since the "a" : "value1",
line doesn't end with a ;
it assumes that the following lines are a continuation of that statement and they should be indented to show that.
:help C-indenting
深入讨论了各种缩进选项及其交互方式.我略读了一下,但没有发现任何意外,但这值得一读.
:help C-indenting
goes into great depth discussing the various indent options and how they interact. I skimmed it and nothing jumped out at me, but it's worth a read.
如果您具有可以更好地识别代码结构的外部格式化程序,则可以始终将equalprg
设置为运行该格式化程序,而不使用内部格式化程序.
If you have an external formatter that better recognizes the structure of your code, you can always set equalprg
to run that instead of using the internal formatter.
再次考虑,set cinoptions+=+0
将禁用缩进以继续行.这也会影响常规代码,但是这可能是一个合理的折衷方案,具体取决于哪个会更使您烦恼.如果要编辑独立的.json文件,也可以按文件类型设置它.
On second thought, set cinoptions+=+0
will disable indenting for line continuation. This will also affect regular code, but it might be a reasonable tradeoff depending which annoys you more. You can also set it per filetype if you're editing standalone .json files.
这篇关于如何在vim中获取gg = G来忽略逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!