# character causes an outdent because you have set
'smartindent', which is designed for C-like languages (where
# indicates the start of a preprocessor directive).
Quoting from the output of
When typing '#' as the first character in a new line, the indent for
that line is removed, the '#' is put in the first column. The indent
is restored for the next line. If you don't want this, use this
mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
When using the ">>" command, lines starting with '#' are not shifted
If you wish to continue using
'cindent' but want a better way of setting it than an autocommand, you can simply place a file containing:
In the location:
Alternatively, you could simply turn off
'smartindent' and use
'autoindent', or, perhaps best, look into obtaining a plugin that is designed specifically to implement Python indentation. Here's one. Here's another.