防止 (g)vim 自动缩进注释
不久前,我必须将
filetype plugin on
我使用的插件放入 .vimrc 中。
但这导致了自动缩进的变化:每当我写下注释“//”,然后按回车键时,vim 自动缩进会自动在下一行输入另一个“//”。
// This is a comment. <ENTER>
// <-- vim automatically puts '// ' there
我可以做什么来避免这种情况? 我在 vim 文件中使用自动缩进设置。 我已经尝试过了,
filetype plugin indent off
但它不起作用。
A while ago, I had to put
filetype plugin on
in my .vimrc for a plugin I use.
But this caused a change in autoindent: Whenever I write a comment "//", and then press enter, vim autoindentation automatically enters another "//" in the next line.
// This is a comment. <ENTER>
// <-- vim automatically puts '// ' there
What can I do to avoid this?
I use the autoindent setting in my vim file.
I already tried
filetype plugin indent off
but it does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在回答你的标题而不是问题的正文,因为你的标题将那些想要阻止 Vim 缩进评论的人带到此页面。
控制 Vim 是否自动缩进新字符的变量是
indentkeys
。我只注意到 Python 和 Yaml 中的缩进不正确,因此我仅对行开头的“#”字符关闭了自动缩进::set indentkeys-=0#
加载文件类型缩进插件将覆盖您所做的任何 .vimrc 设置,您可以设置一个
autocmd
在创建或加载文件后更改缩进键。以下是我的:在评论中,Denilson 指出了一种更简单且可能效果更好的方法(但我目前无法重现该问题):
请参阅 :h indentkeys
请注意,由于(可能)存在错误,如果您使用 Neovim,您还必须指定
filetype插件缩进
,否则文件类型将不会被设置。I am answering your title rather than the body of your question, since your title brings people to this page who are looking to stop Vim from indenting comments.
The variable that controls whether Vim auto-indents a new character is
indentkeys
. I've noticed incorrect indentation only in Python and Yaml, so I've turned off auto-indentation only for the "#" character at the beginning of the line::set indentkeys-=0#
Since loading the filetype indentation plugin will override any .vimrc settings you've made, you can set up an
autocmd
to change the indentkeys after a file is created or loaded. Here are mine:In the comments Denilson pointed out a way that is simpler and may work better (but I can't currently reproduce the problem):
See :h indentkeys
Note that because of (possibly) a bug, if you use Neovim you must also specify
filetype plugin indent on
, or the filetype won't be set.看一下
:h formatoptions
和:h fo-table
。您需要关闭的选项是r
和o
。关闭它们可以防止 vim 在插入模式下按 Enter 或在正常情况下按o
或O
时自动插入注释前导符(在本例中为“//”)模式。Take a look at
:h formatoptions
and:h fo-table
. The options you need to turn off arer
ando
. Turning them off prevents vim from automatically inserting the comment leader (in this case "//") when you press enter in insert mode or when you presso
orO
in normal mode.请参阅
:help 'formatoptions'
- 我知道这有多烦人!试试这个:
See
:help 'formatoptions'
- I know how annoying this is!Try this: