防止(g)vim自动缩进注释

knu*_*nub 11 vim auto-indent

不久前,我不得不放

filetype plugin on
Run Code Online (Sandbox Code Playgroud)

在我的.vimrc中我使用的插件.

但这导致了autoindent的变化:每当我写一个评论"//",然后按回车键,vim autoindentation会在下一行自动输入另一个"//".

// This is a comment. <ENTER>
// <-- vim automatically puts '// ' there
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能避免这种情况?我在我的vim文件中使用autoindent设置.我已经试过了

filetype plugin indent off
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

pio*_*ojo 12

我正在回答你的标题而不是你的问题的正文,因为你的标题将人们带到这个页面,他们希望阻止 Vim 缩进评论。

控制 Vim 是否自动缩进新字符的变量是indentkeys. 我注意到仅在 Python 和 Yaml 中存在不正确的缩进,因此我仅对行首的“#”字符关闭了自动缩进::set indentkeys-=0#

由于加载文件类型缩进插件将覆盖您所做的任何 .vimrc 设置,因此您可以设置autocmd在创建或加载文件后更改缩进键。这是我的:

autocmd BufNewFile,BufReadPost * if &filetype == "python" | set indentkeys-=0# | endif
autocmd BufNewFile,BufReadPost * if &filetype == "yaml" | set expandtab shiftwidth=2 indentkeys-=0# | endif
Run Code Online (Sandbox Code Playgroud)

请参阅:h 缩进键

请注意,由于(可能)一个错误,如果您使用 Neovim,您还必须指定filetype plugin indent on,否则将不会设置文件类型。

  • 感谢您加入这个答案。标题把我带到了这里,它适用于我的 vim 版本。 (2认同)

Mic*_*fik 8

看看:h formatoptions:h fo-table.您需要关闭的选项是ro.关闭它们会阻止vim在插入模式下按Enter键o或按或O在正常模式下时自动插入注释引导符(在本例中为"//").

  • "set formatoptions = -or"实际上并不起作用,即使文档说明了这一点.我现在正在使用"set formatoptions = tnq".谢谢!http://stackoverflow.com/questions/6076592/vim-set-formatoptions-being-lost也很有用. (3认同)
  • @knub `设置格式选项-=o | set formatoptions-=r` 是用于删除选项的语法。`set formatoptions-=ro` 仅当它们在选项字符串中完全连续为“ro”时才有效。 (2认同)

Pet*_*ter 5

:help 'formatoptions'- 我知道这有多烦人!

试试这个:

:set fo-=or
Run Code Online (Sandbox Code Playgroud)