Vim python code formatting

vim

I'm using the plugin auto-pairs to help auto close brackets.

In vim when defining dicts or lists it'll auto formatting like this

| is the cursor position

a_dict = {
        | # the indent is 8 spaces width, but I already set 4 spaces width indent in .vimrc
        }

a_lst = [
        |
        ]

a_lst_with_a_very_loooooooooooong_name = [
        |
        ]

but I want it formats the code like this

lst = [
    |
]

How can I do this?

Best Answer

For Python indenting in vim, I use this plugin for pep8 indentation with Vundle. This plugin works with yours and gives you the functionality you want (at least on my machine)

Example:

lst = [
    |
]

abc = {
    |
}

You especially want the let g:pymode_indent = 0 line in your .vimrc for the pep8 plugin.

Someone who is skilled in vim programming might be able to modify your plugin to do what you need, but this solution might bring you another benefits if you use vim for Python programming.

To address your issue with 8 spaces as a tab - try having all

set tabstop=4
set shiftwidth=4
set expandtab
filetype indent on

in your .vimrc (this is what I have and it gives me 4 spaces indentation)

Related Question