简体   繁体   English

防止 Vim 在 Python 中键入冒号 (:) 时缩进行

[英]Prevent Vim from indenting line when typing a colon (:) in Python

Whenever I append a : character in Vim in Python mode, it either:每当我在 Python 模式下的 Vim 中附加一个:字符时,它要么:

  • indents the line缩进线
  • dedents the line凹痕线
  • does nothing什么都不做

What is it even trying to do, and how do I get rid of this behavior?它甚至试图做什么,我该如何摆脱这种行为?

Certain keys, when pressed, will trigger Vim's indent feature, which will attempt to set the correct amount of indentation on the current line.某些键在按下时会触发 Vim 的缩进功能,该功能将尝试在当前行设置正确的缩进量。 (You can manually trigger this by typing == in normal mode.) (您可以通过在正常模式下键入==来手动触发。)

You can change which keys trigger this behavior, but first you need to know what indenting mode is being used.您可以更改触发此行为的键,但首先您需要知道正在使用哪种缩进模式。

First, execute :set indentexpr?首先,执行:set indentexpr? . . If it is nonempty (I would expect this for Python), then indentexpr mode is being used.如果它是非空的(我希望这适用于 Python),则正在使用indentexpr模式。 In this case, executing :set indentkeys?在这种情况下,执行:set indentkeys? gives you the list of trigger keys.为您提供触发键列表。 To remove the colon, execute :setlocal indentkeys-=: .要删除冒号,请执行:setlocal indentkeys-=:

If indentexpr is empty, then you are probably using cindent mode, and :set cindent?如果indentexpr为空,那么您可能正在使用cindent模式,并且:set cindent? will tell you that cindent is set.会告诉你cindent已设置。 In this case, do the same as before, but using cinkeys instead of indentkeys .在这种情况下,执行与之前相同的操作,但使用cinkeys而不是indentkeys (Note that indentexpr mode takes precedence over cindent mode.) (注意indentexpr模式优先于cindent模式。)

Nathan Grigg's answer set me on the right track.内森·格里格 (Nathan Grigg) 的回答让我走上了正确的道路。 I had to make a few changes for my setup.我不得不对我的设置进行一些更改。

I had to use :setlocal indentkeys-=<:> , because in my case :set indentkeys?我不得不使用:setlocal indentkeys-=<:> ,因为在我的情况下:set indentkeys? showed indentkeys=0{,0},!^F,o,O,e,<:>,=elif,=except .显示indentkeys=0{,0},!^F,o,O,e,<:>,=elif,=except

Also, putting :setlocal indentkeys-=<:> in .vim/after/ftplugin/python.vim did not work to make the change permanent.此外,将:setlocal indentkeys-=<:>放入.vim/after/ftplugin/python.vim并不能使更改永久化。 I found that there is a built-in vim python indent file that runs AFTER this after-ftplugin file.我发现有一个内置的 vim python 缩进文件,它在这个 after-ftplugin 文件之后运行。

To diagnose, open a Python file for editing, and run :scriptnames .要进行诊断,请打开 Python 文件进行编辑,然后运行:scriptnames That will show you a list of all the vim scripts that have run, in order of precedence.这将按优先顺序显示所有已运行的 vim 脚本的列表。 The scripts at the bottom of that list have been applied most recently, and take precedence.该列表底部的脚本是最近应用的,并具有优先权。 See this question on SuperUser for more info.有关更多信息,请参阅SuperUser 上的这个问题

When I did that, it showed me a built-in vim file at /my-install-path/vim/7.4.1830/share/vim/vim74/indent/python.vim .当我这样做时,它在/my-install-path/vim/7.4.1830/share/vim/vim74/indent/python.vim向我显示了一个内置的 vim 文件。 Sure enough, that was setting <:> as part of the indent keys.果然,这是将<:>设置为缩进键的一部分。

To fix it, I set an autocommand in .vimrc, and that really gets the last word.为了修复它,我在 .vimrc 中设置了一个自动命令,这确实是最后的决定。

autocmd FileType python setlocal indentkeys-=<:>

Update更新

I had to add :setlocal indentkeys-=: after all.我不得不添加:setlocal indentkeys-=:毕竟。 Here's what I have in my .vimrc now.这是我现在在我的.vimrc

autocmd FileType python setlocal indentkeys-=<:>
autocmd FileType python setlocal indentkeys-=:

It is trying to be helpful.它正在努力提供帮助。 If you want to turn off all the auto-indenting for the current file,如果要关闭当前文件的所有自动缩进,

:setlocal noautoindent
:setlocal nocindent
:setlocal nosmartindent
:setlocal indentexpr=

Or, you can add set in your vimrc file.或者,您可以在 vimrc 文件中添加 set。 You can do these per file type too.您也可以按文件类型执行这些操作。 See here这里

TL;DR I disabled autoindentation by typing: TL;DR我通过键入以下内容禁用自动缩进:

:set indentexpr= :set indentexpr=

then hitting the ENTER key.然后按 ENTER 键。

It's a quick fix without needing to understand indentkeys ..这是一个快速修复,无需了解缩进键..

Thanks to Christian Long for the docs to indentkeys , where I noticed (my emphasis):感谢Christian Long提供的indentkeys文档,我注意到(我的重点):

A list of keys that, when typed in Insert mode, cause reindenting of the current line.在插入模式下键入时,会导致当前行重新缩进的键列表。 Only happens if 'indentexpr' isn't empty .仅当 'indentexpr' 不为空时才会发生

Tip - you might want to save a copy of the existing value of indentexpr before you clear it out.提示 - 在清除之前,您可能希望保存indentexpr的现有值的副本 To see that (and any other values that are set) just type:要查看(以及设置的任何其他值),只需键入:

:set :设置

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM