Vim 双缩进 python 文件

问题描述 投票:0回答:3

我得到以下行为。鉴于这个

settings.py
片段,从第 33 行点击“o”

31# Application definition
32 
33 INSTALLED_APPS = [
34     'rest_framework',

我明白了

31# Application definition
32 
33 INSTALLED_APPS = [
34         |<-cursor is here, two indents, 8 spaces
35     'rest_framework',

我真正想要的是一个缩进,或者总共 4 个空格,与列表的其余部分保持一致。什么给?我正在使用以下 .vimrc,我主要是从here抄袭的。

set nocompatible              " required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)

" Themes
Plugin 'altercation/vim-colors-solarized'
Plugin 'jnurmine/Zenburn'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

if has('gui_running')
  set background=dark
  colorscheme solarized
else
  colorscheme zenburn
endif

au BufNewFile,BufRead *.py set tabstop=4 shiftwidth=4 textwidth=79 expandtab autoindent fileformat=unix

set encoding=utf-8
set nu
let python_highlight_all=1
syntax on
set backspace=indent,eol,start
python vim
3个回答
9
投票

发生这种情况是因为 Python 默认的 vim 缩进插件。它在

shiftwidth
下面的第一行插入 2
[

您可以在此处查看导致此行为的代码:https://github.com/vim/vim/blob/0b9e4d1224522791c0dbbd45742cbd688be823f3/runtime/indent/python.vim#L74

我建议您安装

vim-python-pep8-indent
插件,它可以根据您的需要在括号内进行缩进。


9
投票

根据@Alik提供的

python.vim
的代码,你可以发现它默认插入了2个
shiftwidth

return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))

但是您可以使用

g:pyindent_open_paren
变量更改此设置。

例如:

let g:pyindent_open_paren=shiftwidth()

或者

let g:pyindent_open_paren=4

0
投票
在 vim 中查看

:h ft-python-indent

 以查看有关 python 缩进的最新文档。一般来说,你应该这样做

filetype plugin indent on let g:python_indent = {} let g:python_indent.open_paren = 'shiftwidth()' let g:python_indent.closed_paren_align_last_line = v:false
然后你应该得到预期的行为。检查 ft-python-indent 帮助页面以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.