Prevent Vim from indenting line when typing a colon (:) in Python
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.)
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-=:
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