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

防止(g)vim自动缩进注释,vim,auto-indent,Vim,Auto Indent,不久前,我不得不 filetype plugin on 在my.vimrc中,我使用了一个插件 但这导致了自动缩进的变化:每当我写注释“/”,然后按enter键时,vim自动缩进会在下一行自动输入另一个“/” // This is a comment. <ENTER> // <-- vim automatically puts '// ' there 但是它不起作用。看看:h formatoptions和:h fo table。需要关闭的选项有r和o。当您在插入模式下按en

不久前,我不得不

filetype plugin on
在my.vimrc中,我使用了一个插件

但这导致了自动缩进的变化:每当我写注释“/”,然后按enter键时,vim自动缩进会在下一行自动输入另一个“/”

// This is a comment. <ENTER>
// <-- vim automatically puts '// ' there

但是它不起作用。

看看
:h formatoptions
:h fo table
。需要关闭的选项有
r
o
。当您在插入模式下按enter键或在正常模式下按
o
o
时,关闭它们可防止vim自动插入注释标题(在本例中为“/”)。

请参见
:帮助“格式选项”
-我知道这有多烦人

试试这个:

:set fo-=or

我回答的是你的标题,而不是你问题的主体,因为你的标题将人们带到了这个页面,他们希望阻止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


请注意,由于(可能)一个bug,如果您使用Neovim,您还必须在上指定
文件类型插件缩进,否则将不会设置文件类型。

我实际上喜欢该功能。每隔一段时间,我必须再次搜索此设置以打开它。:-)“set-formatoptions=-or”实际上不起作用,尽管文档中这么说。我现在正在使用“set formatoptions=tnq”。谢谢也很有用。@knub
set-formatoptions-=o | set-formatoptions-=r
是用于删除选项的语法
set formatoptions-=ro
仅在选项字符串中连续显示为“ro”时有效。
autocmd BufNewFile,BufReadPost * if &filetype == "python" | set indentkeys-=0# | endif
autocmd BufNewFile,BufReadPost * if &filetype == "yaml" | set expandtab shiftwidth=2 indentkeys-=0# | endif