Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim double-indents python files

Tags:

python

vim

I get the following behavior. Given this settings.py snippet, hitting 'o' from line 33

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

I get this

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

What I really want would be one indent, or a total of 4 spaces, in line with the rest of the list. What gives? I'm using the following .vimrc which I mostly cribbed from 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
like image 647
Nils Guillermin Avatar asked Sep 18 '16 03:09

Nils Guillermin


People also ask

How do I indent a Python file in Vim?

Vindect is a Python script to detect the indent options required for editing a Python program. In Vim, output from the :version command should include "+python". The following options are set based upon usage in the current file, and your preferences: shiftwidth , tabstop , softtabstop , smarttab , expandtab .

Does Python care about indents?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.


2 Answers

This happens because of default vim indentation plugin for Python. It inserts 2 shiftwidth on the first line below [.

You can see code which causes this behaviour here: https://github.com/vim/vim/blob/0b9e4d1224522791c0dbbd45742cbd688be823f3/runtime/indent/python.vim#L74

I would recommend you to install vim-python-pep8-indent plugin which does indentation inside parenthesis exactly as you need.

like image 196
Alik Avatar answered Sep 25 '22 14:09

Alik


According to the code of python.vim provided by @Alik, you can notice that it inserts 2 shiftwidth by default.

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

But you can change this using the g:pyindent_open_paren variable.

For instance:

let g:pyindent_open_paren=shiftwidth()

Or

let g:pyindent_open_paren=4
like image 35
Antoine Pinsard Avatar answered Sep 24 '22 14:09

Antoine Pinsard