I'm seeing strange behavior in my windows gvim -- it will continue to indent after a function ends, even once characters in column 0 have been entered:

def foo(k):
  return 42
  """Still indenting"""
# Comment here
  """Still indenting"""

Here is my vimrc:

colorscheme slate

filetype plugin indent on
set tabstop=2
set shiftwidth=2
set expandtab
set ai

set backspace=2
set backspace=indent,eol,start

set cindent
autocmd FileType python setlocal foldmethod=indent smartindent shiftwidth=2 ts=2 et cinwords=if,elif,else,for,while,try,except,finally,def,class

And I also have appended the python vim file found here: https://www.vim.org/scripts/download_script.php?src_id=21056

Removing autoindent/ai so that only smartindent is being used does not seem to solve the problem... any thoughts?

  • I would recommend not setting cindent in your vimrc—it really only works well for C (possibly C-like languages). – D. Ben Knoble Mar 14 '19 at 15:39
  • Python indenting uses a indentexpr. Then, according to :help C-indenting, cindent, autoindent and smartindent are not used. – Ralf Mar 14 '19 at 16:54

I'm afraid there's no solution for that: vim (or even Python) can't detect the end of the function if you don't change the indentation (besides the return 42). The comment on first column may seem strange but is not enough to tell Python that your function has ended:

def test():
    print("on test")
# comment
    print("still on test")
    return 42

If you run print(test()) you will get:

on teste
still on test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.