Reputation: 14245
Whenever I append a :
character in Vim in Python mode, it either:
What is it even trying to do, and how do I get rid of this behavior?
Upvotes: 53
Views: 8473
Reputation: 501
TL;DR I disabled autoindentation by typing:
:set indentexpr=
then hitting the ENTER key.
It's a quick fix without needing to understand indentkeys ..
Thanks to Christian Long for the docs to indentkeys, where I noticed (my emphasis):
A list of keys that, when typed in Insert mode, cause reindenting of the current line. Only happens if 'indentexpr' isn't empty.
Tip - you might want to save a copy of the existing value of indentexpr before you clear it out. To see that (and any other values that are set) just type:
:set
HTH
Upvotes: 1
Reputation: 11514
Nathan Grigg's answer set me on the right track. I had to make a few changes for my setup.
I had to use :setlocal indentkeys-=<:>
, because in my case :set indentkeys?
showed 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.
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.
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.
To fix it, I set an autocommand in .vimrc, and that really gets the last word.
autocmd FileType python setlocal indentkeys-=<:>
Update
I had to add :setlocal indentkeys-=:
after all. Here's what I have in my .vimrc
now.
autocmd FileType python setlocal indentkeys-=<:>
autocmd FileType python setlocal indentkeys-=:
Upvotes: 27
Reputation: 784
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.)
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-=:
.
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.)
Upvotes: 40
Reputation: 19232
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
Upvotes: 7