【发布时间】:2020-08-09 15:37:47
【问题描述】:
我现在正尝试切换到 VIM,并希望让它自动缩进,就像 Python 的 IDE 一样。我有以下 .vimrc 文件
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>
在某些情况下,代码会自动缩进。例如,我尝试过 if 语句和 while 语句,它们在按 Enter 后会缩进。所以下面会正确缩进。
if True:
#this is where my next line automatically starts
while True:
#this is where my next line automatically starts
但是对于类/函数定义,没有缩进。
class Request_Form(QDialog):
#no indentation -- cursor comes here
谁能帮我纠正这种行为
【问题讨论】:
-
你检查过这个答案吗:stackoverflow.com/questions/65076/…
-
如果你使用
:filetype,它会回复indent:ON吗?编辑 Python 文件时,:set indentexpr?是否回复indentexpr=GetPythonIndent(v:lnum)或类似的? -
看起来您正在从
'smartindent'获得缩进,这将(以及其他规则)在'cinwords'中的单词之后缩进,默认为if,else,while,do,for,switch,这样就解释了您所看到的...但是'smartindent'并不适合 Python,您需要使用filetype indent on加载每个文件类型的缩进规则,当您编辑 Python 源代码时,它应该为 Python 加载适当的缩进。 -
谢谢你,@filbranden,为我做了这件事。
-
@Dargscisyhp 感谢您的确认!我将其发布为答案。