2

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

Dargscisyhp
  • 103
  • 1
  • 10
  • Did you check this answer : https://stackoverflow.com/questions/65076/how-do-i-set-up-vim-autoindentation-properly-for-editing-python-files – jossefaz Apr 26 '20 at 05: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 Apr 26 '20 at 05:27
  • 2
    Looks like you're getting indentation from [`'smartindent'`](https://vimhelp.org/options.txt.html#%27smartindent%27), which will (among other rules) indent after words in [`'cinwords'`](https://vimhelp.org/options.txt.html#%27cinwords%27), 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 Apr 26 '20 at 05:32
  • Thank you, @filbranden, that did it for me. – Dargscisyhp Apr 26 '20 at 05:39
  • @Dargscisyhp Thanks for confirming! I posted that as an answer. – filbranden Apr 26 '20 at 17:18

1 Answers1

6

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.

filbranden
  • 8,522
  • 2
  • 16
  • 32