Skip to content

vim plugin: provide black-compatible indentexpr #3151

Closed

Description

@kalekundert

The way that vim automatically indents python code1 differs from the black format. I'm aware of two differences: (i) lines within parentheses/braces are indented twice rather than once and (ii) closing parentheses/braces are not dedented. These differences can be seen clearly by looking at a function argument list that spans multiple lines. Here is such a list as it is automatically formatted by vim (i.e. typed without hitting tab or backspace):

# $ vim -u /etc/vimrc +'filetype indent on' +'set ft=python'
def foo(
        a,
        b,
        c,
        ):
    pass

Here is the same argument list as it would be formatted by black (ignoring that it would be condensed onto a single line):

def foo(
    a,
    b,
    c,
):
    pass

Multiline list/dict/set comprehensions have the same behavior.

I personally find it annoying that the code I type does not immediately conform to the black style. As such, I think it'd be nice if the vim plugin provided by this repository included an indentexpr that implemented the black indentation rules.

Is there any interest in having something like this? I have some experience writing vim plugins, so I might be able to contribute something, but probably not in the near future.

Alternatively, is there an easy way to get vim to do what I want without having to write a bunch of vimscript code? A brief search didn't turn up any existing plugins or relevant vimrc settings, but this feels like it should be a common desire and I'm half-convinced that I'm missing something simple.

Footnotes

  1. As specified by /usr/share/vim/vim82/indent/python.vim. ↩

Activity

  1. added
    S: acceptedThe changes in this design / enhancement issue have been accepted and can be implemented
    on Jul 1, 2022
  2. ichard26 commented on Jul 1, 2022

    @ichard26
    Collaborator

    I would love to see such a configuration sample in the docs. I don't think anyone in the core team can actually write vimscript or anything similar though so we would be dependent on external contributions.

  3. kalekundert commented on Jul 1, 2022

    @kalekundert
    Author

    Alright, I'm glad there's some interest. I might make a PR if I ever get some time, but if anyone else is interested in working on this, don't let me stop you.

  4. aus-hawk commented on Sep 18, 2022

    @aus-hawk

    vim-python-pep8-indent is a single file plugin that gets the job done. Should it be recommended to Vim users in the docs or should it just be added to the Black plugin itself?

  5. tomatotomata commented on Aug 2, 2026

    @tomatotomata
    Contributor

    I was thinking of taking the documentation path mentioned here: add a short Vim note recommending vim-python-pep8-indent for insert-mode indentation that matches Black, while keeping Black as the formatter. That keeps the integration small and avoids duplicating a Vimscript indentation engine in this repository. Let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: vimVim pluginS: acceptedThe changes in this design / enhancement issue have been accepted and can be implementedT: enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions