问题
I want to use vim to write python code but there is a problem on auto indention. First I downloaded the latest python.vim from http://www.vim.org/scripts/script.php?script_id=790 and putted it in the correct dir. Then I edited my 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 like 'for', 'if', 'while' can autoindent perfectly. But it doesn't work on 'def', 'try', 'except'. What should I do? Thank you very much.
回答1:
I have this line in my vimrc for long time, don't know if there is better way nowadays. but you could at least give it a 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 have
filetype plugin indent on
too
回答2:
That vim script you linked to doesn't do any auto-indentation, only syntax highlighting.
The auto-indentation you are observing is the one that's built into vim, it is designed for coding C, and it only recognizes the keywords described here:
http://vimdoc.sourceforge.net/htmldoc/options.html#%27cinwords%27
That's why it works for if
and while
but not def
(there's no def
in C).
You turned it on with set cindent
.
You may want to try another script like this one:
http://www.vim.org/scripts/script.php?script_id=974
来源:https://stackoverflow.com/questions/16570737/auto-indent-doesnt-work-when-using-vim-coding-python