vim

How to stop vim changiing indenting when I type "or" or "and"?


When I am entering perl code, vim enforces the indenting of the line when I type "or" or "and" on lines with only whitespace. If the cursor is inside the same (), [], or {} as the last line of code or data, it adds indenting to one tabstop more than that line. If the cursor is outside the (), [] or {}, the indent is enforced to the same indent as that last line, even though I now want -- and had -- one tabstop less.

This

my %order_data = (
    order_num => 98765,
    <type "or" here>

ends up as

my %order_data = (
    order_num => 98765,
        or
#       ^ Indented too far

This

my %order_data = (
    order_num => 98765,
    order_date => today(),
);
<type "or" here>

ends up as

my %order_data = (
    order_name => 98765,
    order_date => today(),
);
    or
#   ^ Indented too far

This happens as soon as the "r" (or the "d" in "and") is typed. It is not like when abbreviations are expanded, and whitespace or punctuation has to be entered to "finish" the abbreviation and expand it.

Yes, this is a minor annoyance, but those examples are not random. I have to type the prefix "order_" dozens of times a day, and have to switch modes, unindent the code, and switch modes back every single time.

The smartindent setting is on, and the cindent setting is off.

Please tell me how to turn this off, or what I have to modify to prevent this extra indenting and have entering "or" or "and" work just like every other word.

Thank you.


Solution

  • First, you can unindent without switching mode by pressing <C-d> (and <C-t> to indent, see :help i_ctrl-d and :help i_ctrl-t).

    Second, that behavior depends on the :help 'indentkeys' option, which has this value by default for Perl files:

    0{,0},0),0],:,0#,!^F,o,O,e,0=,0),0],0=or,0=and
    

    I guess you can spot the "problem", here.

    You can simply remove the parts you don't want with:

    :set indentkeys-=0=or
    :set indentkeys-=0=and
    

    To have that done automatically for you when you edit a Perl file, create ~/.vim/after/ftplugin/perl.vim if it doesn't exist already, with the following content:

    setlocal indentkeys-=0=or
    setlocal indentkeys-=0=and
    

    Note the use of :help :setlocal to ensure that the option doesn't leak to other buffers.