Vim双缩进python文件

时间:2016-09-18 03:37:28

标签: python vim

我得到以下行为。鉴于此settings.py代码段,请点击“' o'来自第33行

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

2 个答案:

答案 0 :(得分:3)

这是因为Python的默认vim缩进插件。它会在Dimension size; protected void paintComponent(Graphics g){ if (!size.equals(getSize()){ size = getSize(); } } 下方的第一行插入2 shiftwidth

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

我建议您安装vim-python-pep8-indent插件,它会在您的括号内完全按照您的需要进行缩进。

答案 1 :(得分:1)

根据@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