Autoindent : smartindent and indentexpr

社会主义新天地 提交于 2019-12-24 01:19:45

问题


When I'm editing a Python file, for example:

def my_func():
    print('Something')
    <-- CURSOR IS HERE

and I want to add a comment by typing a #, the line is automatically reindenting to the very beginning of the line:

def my_func():
    print('Something')
#<-- CURSOR IS HERE

I found that it's an effect of the smartindent option, so to fix it, I just have to run :set nosi (or disable it in my .vimrc).

But in Vim's help, in :h 'smartindent', you can read this:

When 'cindent' is on or 'indentexpr' is set, setting 'si' has no effect.

But my indentexpr option is set to the following:

:set indentexpr?
indentexpr=GetPythonIndent(v:lnum)

I certainly should avoid using smartindent option at all, since it looks to be an old feature, and is designed to work with C-style languages only;

But I wonder why smartindent does have some effect when I'm editing python files, considering what is written in the help?


回答1:


Yes, It does behaved the same way you explained.

In my case :echo GetPythonIndent(v:lnum) returns -1

:h indentexpr has following text explaining the behaviour.

 The expression must return the number of spaces worth of indent. It
 can return "-1" to keep the current indent (this means 'autoindent' is
 used for the indent).

As we set si, it takes over ai.

Now :h si suggest a work around:

:inoremap # X^H#

where ^H is entered as Ctrl + V Ctrl + H

I am sure you will have a better solution than the work around provided



来源:https://stackoverflow.com/questions/35156448/autoindent-smartindent-and-indentexpr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!