[python] How do I set up Vim autoindentation properly for editing Python files?

I've trouble setting up Vim (7.1.xxx) for editing Python files (*.py). Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.

This question is related to python vim configuration spaces

The answer is


I use this on my macbook:

" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set expandtab
au BufRead,BufNewFile *.h set expandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler                           " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line

(edited to only show stuff related to indent / tabs)


Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms.

In vim type

:help vimrc

and check your path to the _vimrc/.vimrc file with

:echo $HOME

:echo $VIM

Make sure you are only using one file. If you want to split your configuration into smaller chunks you can source other files from inside your _vimrc file.

:help source


A simpler option: just uncomment the following part of the configuration (which is originally commented out) in the /etc/vim/vimrc file:

    if has("autocmd")
      filetype plugin indent on
    endif

Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms.

In vim type

:help vimrc

and check your path to the _vimrc/.vimrc file with

:echo $HOME

:echo $VIM

Make sure you are only using one file. If you want to split your configuration into smaller chunks you can source other files from inside your _vimrc file.

:help source


I use the vimrc in the python repo among other things:

http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc

I also add

set softtabstop=4

I have my old config here that I'm updating


I use:

$ cat ~/.vimrc
syntax on
set showmatch
set ts=4
set sts=4
set sw=4
set autoindent
set smartindent
set smarttab
set expandtab
set number

But but I'm going to try Daren's entries


I use this on my macbook:

" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set expandtab
au BufRead,BufNewFile *.h set expandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler                           " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line

(edited to only show stuff related to indent / tabs)


for more advanced python editing consider installing the simplefold vim plugin. it allows you do advanced code folding using regular expressions. i use it to fold my class and method definitions for faster editing.


Combining the solutions proposed by Daren and Thanos we have a good .vimrc file.

_x000D_
_x000D_
-----_x000D_
" configure expanding of tabs for various file types_x000D_
au BufRead,BufNewFile *.py set expandtab_x000D_
au BufRead,BufNewFile *.c set noexpandtab_x000D_
au BufRead,BufNewFile *.h set noexpandtab_x000D_
au BufRead,BufNewFile Makefile* set noexpandtab_x000D_
_x000D_
" --------------------------------------------------------------------------------_x000D_
" configure editor with tabs and nice stuff..._x000D_
" --------------------------------------------------------------------------------_x000D_
set expandtab           " enter spaces when tab is pressed_x000D_
set textwidth=120       " break lines when line length increases_x000D_
set tabstop=4           " use 4 spaces to represent tab_x000D_
set softtabstop=4_x000D_
set shiftwidth=4        " number of spaces to use for auto indent_x000D_
set autoindent          " copy indent from current line when starting a new line_x000D_
set smartindent_x000D_
set smarttab_x000D_
set expandtab_x000D_
set number_x000D_
_x000D_
" make backspaces more powerfull_x000D_
set backspace=indent,eol,start_x000D_
_x000D_
set ruler                           " show line and column number_x000D_
syntax on               " syntax highlighting_x000D_
set showcmd             " show (partial) command in status line
_x000D_
_x000D_
_x000D_


Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms.

In vim type

:help vimrc

and check your path to the _vimrc/.vimrc file with

:echo $HOME

:echo $VIM

Make sure you are only using one file. If you want to split your configuration into smaller chunks you can source other files from inside your _vimrc file.

:help source


for more advanced python editing consider installing the simplefold vim plugin. it allows you do advanced code folding using regular expressions. i use it to fold my class and method definitions for faster editing.


I use the vimrc in the python repo among other things:

http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc

I also add

set softtabstop=4

I have my old config here that I'm updating


Combining the solutions proposed by Daren and Thanos we have a good .vimrc file.

_x000D_
_x000D_
-----_x000D_
" configure expanding of tabs for various file types_x000D_
au BufRead,BufNewFile *.py set expandtab_x000D_
au BufRead,BufNewFile *.c set noexpandtab_x000D_
au BufRead,BufNewFile *.h set noexpandtab_x000D_
au BufRead,BufNewFile Makefile* set noexpandtab_x000D_
_x000D_
" --------------------------------------------------------------------------------_x000D_
" configure editor with tabs and nice stuff..._x000D_
" --------------------------------------------------------------------------------_x000D_
set expandtab           " enter spaces when tab is pressed_x000D_
set textwidth=120       " break lines when line length increases_x000D_
set tabstop=4           " use 4 spaces to represent tab_x000D_
set softtabstop=4_x000D_
set shiftwidth=4        " number of spaces to use for auto indent_x000D_
set autoindent          " copy indent from current line when starting a new line_x000D_
set smartindent_x000D_
set smarttab_x000D_
set expandtab_x000D_
set number_x000D_
_x000D_
" make backspaces more powerfull_x000D_
set backspace=indent,eol,start_x000D_
_x000D_
set ruler                           " show line and column number_x000D_
syntax on               " syntax highlighting_x000D_
set showcmd             " show (partial) command in status line
_x000D_
_x000D_
_x000D_


I use this on my macbook:

" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set expandtab
au BufRead,BufNewFile *.h set expandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler                           " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line

(edited to only show stuff related to indent / tabs)


Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms.

In vim type

:help vimrc

and check your path to the _vimrc/.vimrc file with

:echo $HOME

:echo $VIM

Make sure you are only using one file. If you want to split your configuration into smaller chunks you can source other files from inside your _vimrc file.

:help source


for more advanced python editing consider installing the simplefold vim plugin. it allows you do advanced code folding using regular expressions. i use it to fold my class and method definitions for faster editing.


I use:

$ cat ~/.vimrc
syntax on
set showmatch
set ts=4
set sts=4
set sw=4
set autoindent
set smartindent
set smarttab
set expandtab
set number

But but I'm going to try Daren's entries


A simpler option: just uncomment the following part of the configuration (which is originally commented out) in the /etc/vim/vimrc file:

    if has("autocmd")
      filetype plugin indent on
    endif

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to vim

Why does using from __future__ import print_function breaks Python2-style print? How to run vi on docker container? How can I install MacVim on OS X? Find and replace strings in vim on multiple lines Running Python code in Vim How do I set the default font size in Vim? Move cursor to end of file in vim Set encoding and fileencoding to utf-8 in Vim How to select all and copy in vim? Why I've got no crontab entry on OS X when using vim?

Examples related to configuration

My eclipse won't open, i download the bundle pack it keeps saying error log Getting value from appsettings.json in .net core pgadmin4 : postgresql application server could not be contacted. ASP.NET Core configuration for .NET Core console application Turning off eslint rule for a specific file PHP Warning: Module already loaded in Unknown on line 0 How to read AppSettings values from a .json file in ASP.NET Core How to store Configuration file and read it using React Hadoop cluster setup - java.net.ConnectException: Connection refused Maven Jacoco Configuration - Exclude classes/packages from report not working

Examples related to spaces

Visual Studio Code - Convert spaces to tabs Removing spaces from a variable input using PowerShell 4.0 Sublime Text 3, convert spaces to tabs Read input numbers separated by spaces How do I convert a list into a string with spaces in Python? How to copy directories with spaces in the name How can I convert tabs to spaces in every file of a directory? How can I convert spaces to tabs in Vim or Linux? Adding whitespace in Java Converting a sentence string to a string array of words in Java