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

时间:2022-11-22 21:05:00

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?

它甚至试图做什么,我该如何摆脱这种行为?

3 个解决方案

#1


18  

Certain keys, when pressed, will trigger Vim's indent feature, which will attempt to set the correct amount of indentation on the current line. (You can manually trigger this by typing == in normal mode.)

按下某些键时,将触发Vim的缩进功能,该功能将尝试在当前行上设置正确的缩进量。 (您可以通过在正常模式下键入==来手动触发此操作。)

You can change which keys trigger this behavior, but first you need to know what indenting mode is being used.

您可以更改哪些键触发此行为,但首先您需要知道正在使用的缩进模式。

First, execute :set indentexpr?. If it is nonempty (I would expect this for Python), then indentexpr mode is being used. In this case, executing :set indentkeys? gives you the list of trigger keys. To remove the colon, execute :setlocal indentkeys-=:.

首先,执行:set indentexpr?。如果它是非空的(我希望这对于Python),那么正在使用indentexpr模式。在这种情况下,执行:set indentkeys?为您提供触发键列表。要删除冒号,请执行:setlocal indentkeys- =:。

If indentexpr is empty, then you are probably using cindent mode, and :set cindent? will tell you that cindent is set. In this case, do the same as before, but using cinkeys instead of indentkeys. (Note that indentexpr mode takes precedence over cindent mode.)

如果indentexpr为空,那么你可能正在使用cindent模式,并且:set cindent?会告诉你cindent已经确定。在这种情况下,请执行与以前相同的操作,但使用cinkeys而不是缩进键。 (请注意,indentexpr模式优先于cindent模式。)

#2


7  

Nathan Grigg's answer set me on the right track. I had to make a few changes for my setup.

Nathan Grigg的回答让我走上正轨。我必须为我的设置做一些更改。

I had to use :setlocal indentkeys-=<:>, because in my case :set indentkeys? showed indentkeys=0{,0},!^F,o,O,e,<:>,=elif,=except.

我不得不使用:setlocal indentkeys - = <:>,因为在我的情况下:设置缩进键?显示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. I found that there is a built-in vim python indent file that runs AFTER this after-ftplugin file.

另外,在.vim / after / ftplugin / python.vim中放置:setlocal indentkeys - = <:>无法使更改成为永久更改。我发现有一个内置的vim python缩进文件,它在这个after-ftplugin文件之后运行。

To diagnose, open a Python file for editing, and run :scriptnames. That will show you a list of all the vim scripts that have run, in order of precedence. The scripts at the bottom of that list have been applied most recently, and take precedence. See this question on SuperUser for more info.

要进行诊断,请打开Python文件进行编辑,然后运行:scriptnames。这将按优先顺序显示已运行的所有vim脚本的列表。该列表底部的脚本最近已应用,并且优先。有关详细信息,请参阅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. Sure enough, that was setting <:> as part of the indent keys.

当我这样做时,它在/my-install-path/vim/7.4.1830/share/vim/vim74/indent/python.vim上显示了一个内置的vim文件。果然,这是将<:>设置为缩进键的一部分。

To fix it, I set an autocommand in .vimrc, and that really gets the last word.

为了解决这个问题,我在.vimrc中设置了一个autocommand,这真的得到了最后一个字。

autocmd FileType python setlocal indentkeys-=<:>

Update

I had to add :setlocal indentkeys-=: after all. Here's what I have in my .vimrc now.

我不得不补充:setlocal indentkeys- =:毕竟。这就是我现在在.vimrc中的内容。

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

#3


6  

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. You can do these per file type too. See here

或者,您可以在vimrc文件中添加set。您也可以按文件类型执行这些操作。看这里

#1


18  

Certain keys, when pressed, will trigger Vim's indent feature, which will attempt to set the correct amount of indentation on the current line. (You can manually trigger this by typing == in normal mode.)

按下某些键时,将触发Vim的缩进功能,该功能将尝试在当前行上设置正确的缩进量。 (您可以通过在正常模式下键入==来手动触发此操作。)

You can change which keys trigger this behavior, but first you need to know what indenting mode is being used.

您可以更改哪些键触发此行为,但首先您需要知道正在使用的缩进模式。

First, execute :set indentexpr?. If it is nonempty (I would expect this for Python), then indentexpr mode is being used. In this case, executing :set indentkeys? gives you the list of trigger keys. To remove the colon, execute :setlocal indentkeys-=:.

首先,执行:set indentexpr?。如果它是非空的(我希望这对于Python),那么正在使用indentexpr模式。在这种情况下,执行:set indentkeys?为您提供触发键列表。要删除冒号,请执行:setlocal indentkeys- =:。

If indentexpr is empty, then you are probably using cindent mode, and :set cindent? will tell you that cindent is set. In this case, do the same as before, but using cinkeys instead of indentkeys. (Note that indentexpr mode takes precedence over cindent mode.)

如果indentexpr为空,那么你可能正在使用cindent模式,并且:set cindent?会告诉你cindent已经确定。在这种情况下,请执行与以前相同的操作,但使用cinkeys而不是缩进键。 (请注意,indentexpr模式优先于cindent模式。)

#2


7  

Nathan Grigg's answer set me on the right track. I had to make a few changes for my setup.

Nathan Grigg的回答让我走上正轨。我必须为我的设置做一些更改。

I had to use :setlocal indentkeys-=<:>, because in my case :set indentkeys? showed indentkeys=0{,0},!^F,o,O,e,<:>,=elif,=except.

我不得不使用:setlocal indentkeys - = <:>,因为在我的情况下:设置缩进键?显示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. I found that there is a built-in vim python indent file that runs AFTER this after-ftplugin file.

另外,在.vim / after / ftplugin / python.vim中放置:setlocal indentkeys - = <:>无法使更改成为永久更改。我发现有一个内置的vim python缩进文件,它在这个after-ftplugin文件之后运行。

To diagnose, open a Python file for editing, and run :scriptnames. That will show you a list of all the vim scripts that have run, in order of precedence. The scripts at the bottom of that list have been applied most recently, and take precedence. See this question on SuperUser for more info.

要进行诊断,请打开Python文件进行编辑,然后运行:scriptnames。这将按优先顺序显示已运行的所有vim脚本的列表。该列表底部的脚本最近已应用,并且优先。有关详细信息,请参阅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. Sure enough, that was setting <:> as part of the indent keys.

当我这样做时,它在/my-install-path/vim/7.4.1830/share/vim/vim74/indent/python.vim上显示了一个内置的vim文件。果然,这是将<:>设置为缩进键的一部分。

To fix it, I set an autocommand in .vimrc, and that really gets the last word.

为了解决这个问题,我在.vimrc中设置了一个autocommand,这真的得到了最后一个字。

autocmd FileType python setlocal indentkeys-=<:>

Update

I had to add :setlocal indentkeys-=: after all. Here's what I have in my .vimrc now.

我不得不补充:setlocal indentkeys- =:毕竟。这就是我现在在.vimrc中的内容。

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

#3


6  

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. You can do these per file type too. See here

或者,您可以在vimrc文件中添加set。您也可以按文件类型执行这些操作。看这里