问题
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