Vim getting indentation wrong for Python code (with the python-mode Plugin)
Asked Answered
P

2

7

As shown in the following screen-cast, I demonstrate the strange behaviors of Vim when it comes to adding comments to Python code. Between Demo 1 and Demo 2, I was only starting the line of comments at different indentation level (inside a class definition).

Demo 1

enter image description here

Demo 2

enter image description here

The problem:

As demonstrated towards the end of screen-cast, only typing : in insert mode will get the indentation right.

An ideal solution would achieve:

That when starting a line of comment, please leave the indentation level correctly;

Presidentelect answered 3/7, 2017 at 23:31 Comment(5)
please show me your vimrc. especially, indentkeys, indentexpr, installed plugins,Karren
@mattn, it seems to be more of a incompatibility problem due to set smartindent? I no longer have the problem as described above after simply removing such setting.Presidentelect
Ah, it seems smartindent affected. If you don't remember set the option, you can confirm where is the setting with :verbose set smartindent . And if you can disable it for filetype=python with autocmd FileType python setlocal nosmartindentKarren
Possible duplicate of Vim automatically removes indentation on Python commentsVociferate
I would agree this is a duplicate. Shall I delete this question?@VociferatePresidentelect
S
7

This sounds like a vim setting issue. Starting with vanilla vim, there is no indenting on each line, when I create a new line. If I create a file called .vimrc in my home directory. i.e. ~/.vimrc and put

filetype indent plugin on

in the file, I get what seems like expected indenting behavior. Does that work for you.

Spoilsman answered 4/7, 2017 at 0:54 Comment(0)
P
2

Solution in my case

I commented out the following line, and everything went on fine. No further weird corrections was made to the comments. set smartindent

Other info of my Vim:

For the sake of indentation, here goes the settings:

filetype plugin on
filetype indent on
set autoindent

Also, it has been verified that python-mode is not at fault. With the python-mode or without, set smartindent keeps getting me the problem.

Tentative conclusion:

set smartindent is not working well, probably, with my folding method specified as follows. Such setting is placed in C:/vim/vimfiles/ftplugin/python.vim

setlocal formatoptions=crnqj

Here goes the correspondence of the characters used, yet none of which seems to be a trouble.

  • c: Auto-wrap comments using textwidth, inserting the current comment leader automatically.

  • r: Automatically insert the current comment leader after hitting <Enter> in Insert mode.

  • n: When formatting text, recognize numbered lists. This actually uses the 'formatlistpat' option, thus any kind of list can be used. The indent of the text after the number is used for the next line. The default is to find a number, optionally followed by '.', ':', ')', ']' or '}'. Note that 'autoindent' must be set too. Doesn't work well together with "2". Example:

    1. the first item
       wraps
    2. the second item
    
  • q: Allow formatting of comments with "gq". Note that formatting will not change blank lines or lines containing only the comment leader. A new paragraph starts after such a line, or when the comment leader changes.

  • j: Where it makes sense, remove a comment leader when joining lines.

Reference to a better framed + answered solution

In short, what I observed when editing a Python script is not unique to the "Python filetype" alone. It has to do with the symbol #, aka "hash".

Presidentelect answered 4/7, 2017 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.