5

I'm attempting to switch over to VIM right now, and would like to get it to indent automatically as an IDE would for Python. I've got the following .vimrc file

syntax on

set number
autocmd FileType tex,latex,python set showmatch

nnoremap j gj
nnoremap k gk

"Python Settings
autocmd FileType python set softtabstop=4
autocmd FileType python set tabstop=4
autocmd FileType python set autoindent
autocmd FileType python set expandtab
autocmd FileType python set textwidth=80
autocmd FileType python set smartindent
autocmd FileType python set shiftwidth=4
autocmd FileType python map <buffer> <F2> :w<CR>:exec '! python' shellescape(@%, 1)<CR>
autocmd FileType python imap <buffer> <F2> <esc>:w<CR>:exec '! python' shellescape(@%, 1)<CR>

The code automatically indents in some cases. For instance, I've tried if statements and while statements, which after hitting enter are indented. So the following will indent properly.

if True:
    #this is where my next line automatically starts
while True:
    #this is where my next line automatically starts

But for class/function definitions, there is no indentation.

class Request_Form(QDialog):
#no indentation -- cursor comes here

Could anyone help me correct this behavior

5
  • Did you check this answer : stackoverflow.com/questions/65076/…
    – jossefaz
    Commented Apr 26, 2020 at 5:06
  • If you use :filetype, does it reply with indent:ON? When editing a Python file, does :set indentexpr? reply with indentexpr=GetPythonIndent(v:lnum) or similar?
    – filbranden
    Commented Apr 26, 2020 at 5:27
  • 2
    Looks like you're getting indentation from 'smartindent', which will (among other rules) indent after words in 'cinwords', which default to if,else,while,do,for,switch, so that explains what you're seeing... But 'smartindent' is not really appropriate for Python, you'll want to use filetype indent on to load per-filetype indentation rules, which should load appropriate indentation for Python when you edit Python sources.
    – filbranden
    Commented Apr 26, 2020 at 5:32
  • Thank you, @filbranden, that did it for me. Commented Apr 26, 2020 at 5:39
  • @Dargscisyhp Thanks for confirming! I posted that as an answer.
    – filbranden
    Commented Apr 26, 2020 at 17:18

1 Answer 1

8

Looks like you're getting indentation from 'smartindent', which will (among other rules, mainly related to curly braces) indent after lines that include words in the 'cinwords' list, which defaults to if,else,while,do,for,switch, so that seems to explain exactly what you're currently seeing.

But 'smartindent' is not really that appropriate for Python (as you can guess by it handling curly braces.) Instead, you'll want to use filetype indent on in order to load per-filetype indentation rules, which should load appropriate indentation for Python when you edit Python sources.

I'd recommend that you add this to your vimrc:

filetype plugin indent on

That should solve the issue for you.

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