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

发布于 2025-01-06 11:50:08 字数 382 浏览 3 评论 0原文

不久前,我必须将

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就是爱搞怪 2025-01-13 11:50:08

我正在回答你的标题而不是问题的正文,因为你的标题将那些想要阻止 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

在评论中,Denilson 指出了一种更简单且可能效果更好的方法(但我目前无法重现该问题):

autocmd FileType yaml,python setlocal indentkeys-=0#

请参阅 :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:

autocmd BufNewFile,BufReadPost * if &filetype == "python" | set indentkeys-=0# | endif
autocmd BufNewFile,BufReadPost * if &filetype == "yaml" | set expandtab shiftwidth=2 indentkeys-=0# | endif

In the comments Denilson pointed out a way that is simpler and may work better (but I can't currently reproduce the problem):

autocmd FileType yaml,python setlocal indentkeys-=0#

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.

烟酒忠诚 2025-01-13 11:50:08

看一下 :h formatoptions:h fo-table。您需要关闭的选项是 ro。关闭它们可以防止 vim 在插入模式下按 Enter 或在正常情况下按 oO 时自动插入注释前导符(在本例中为“//”)模式。

Take a look at :h formatoptions and :h fo-table. The options you need to turn off are r and o. Turning them off prevents vim from automatically inserting the comment leader (in this case "//") when you press enter in insert mode or when you press o or O in normal mode.

定格我的天空 2025-01-13 11:50:08

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

试试这个:

:set fo-=or

See :help 'formatoptions' - I know how annoying this is!

Try this:

:set fo-=or
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文