Auto indentation doesn't work when using python vim coding

I want to use vim to write python code, but there is a problem with auto-indentation. First, I downloaded the latest version of python.vim from http://www.vim.org/scripts/script.php?script_id=790 and put it in the correct directory. Then I edited vimrc.

syntax on set nu set tabstop=4 set softtabstop=4 set shiftwidth=4 "set cindent set autoindent set smartindent set expandtab set filetype=python au BufNewFile,BufRead *.py,*.pyw setf python 

Now I find that keywords such as "for", "if", "at the time" can be automatically processed. But this does not work on "def", "try", "except". What should I do? Thank you very much.

+6
source share
2 answers

I have this line in my vimrc for a long time, I don't know if there is a better way these days. but you could at least try.

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

and I

 filetype plugin indent on 

too

+7
source

The fact that the vim script you contacted does not indent automatically, only syntax highlighting.

The automatic indentation that you observe is the one that is built into vim, it is designed to encode C, and it only recognizes the keywords described here:

http://vimdoc.sourceforge.net/htmldoc/options.html#%27cinwords%27

This is why it works for if and while , but not def (there is no def in C). You turned it on with set cindent .

You can try another script, like this one:

http://www.vim.org/scripts/script.php?script_id=974

+1
source

Source: https://habr.com/ru/post/945084/


All Articles