Reputation: 66
This happens even with an empty .vimrc
When on a file with a .html extension, if it is currently in this state:
<p>
t
</p>
and I put my cursor over the "t", press a (append)
<p>
t_
</p>
and then type "{", the line will indent left and result in this:
<p>
t{_
</p>
with my cursor still in insert mode at "_"
I've tried looking up "indent" and "{" in various vim help files but I can't find anything on why it automatically indents the line to the left.
Is there any way to disable this auto left indent?
Upvotes: 3
Views: 742
Reputation: 705
Your indenting is being handled by a function called HtmlIndent()
. If you open vim with vim --clean testIndent.html
from the command line, then type :set indentexpr?
in vim and hit enter, you will see indentexpr=HtmlIndent()
. See :h indentexpr
to read more about what this setting means. As for what the function does, you would need to look at where it is defined. You can type :e $VIMRUNTIME/**/indent/html.vim
into vim and (before hitting enter) hit tab. Vim should automatically fill in the location of vim's html indent file. For me on Ubuntu with vim 8.2, this is at /usr/local/share/vim/vim82/indent/html.vim
. The HtmlIndent()
function is defined there.
That is the first part of the answer. The second part answers why your indenting is triggered by typing the {
character. HtmlIndent()
determines what the indent of a line should be, but the indentkeys
setting triggers a line's proper indent to be evaluated and set when certain keys are typed. See :h indentkeys
. If you open vim again with vim --clean testIndent.html
and type :set indentkeys?
, you will see something like indentkeys=o,O,<Return>,<>>,{,},!^F
. The {
character in that setting is causing vim to evaluate, through HtmlIndent()
, what it believes is the proper indent for the line you are on every time you type {
.
Given this, you have several options. You could look for someone who has made their own addition to vim's html indenting settings. You could simply disable this automatic indenting. You could attempt to modify vim's HtmlIndent()
function. One very simple option for you is to remove the {
character from the indentkeys
setting. You can do this with :set indentkeys-={
. Then do :set indentkeys?
again to verify that {
has been removed. You could remove }
the same way. Note that this will only prevent vim from automatically re-indenting a line every time you type the {, }
characters. It will not actually change vim's opinion on what that line's indent should be, so in other cases where vim evaluates and sets a line's indent, it will still do so in a way you disagree with.
Upvotes: 4