tabs for indentation in python files in vim

  • Last Update :
  • Techknowledgy :

The Indent Finder plugin sets the correct indentation for each file that you edit. It uses a Python script that determines what indent style was used, then adjusts your settings so that new modifications use the same style. ,You may need to maintain source files that use different indentation styles. For example, some files may use four spaces for each indent, while others use three spaces, or use tabs, or use a mixture of tabs and spaces. ,You could put the following in your vimrc and use a mapping to call PyIndentAutoCfg() when wanted, or define an auto command to call that function when a Python file is opened. ,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 commands need to be executed once per Vim session (done below):

" First need to set the Python path so is found.: py
import vindect
   : py vindect.setDefaults(...)
" optional to set different defaults

This command detects and sets the indent options when a new file is opened (done below):

: py vindect.detect(preferred = 'space')
  • Place in your .vim (Unix based systems) or vimfiles (Windows) directory.
  • Create an after/ftplugin/python.vim file with the following contents. On Windows systems, change .vim to vimfiles.
if !exists('s:configured_vindect')
if has('python')
import sys, os;
import vindect
let s: configured_vindect = 1
let s: configured_vindect = 0
   " to set different defaults: py vindect.setDefaults(...)
   let s: configured_vindect = 0
if s: configured_vindect
py vindect.detect(preferred = 'space')

Re the Vim script: The following version of PyIsTabIndent() handles more languages:

function !PyIsTabIndent()
let lnum = 1
while lnum <= 100
let line = getline(lnum)
let lnum = lnum + 1
if line = ~'^\t\t\(if\|while\|do\|for\|public\|private\|char\|int\|float\|double\|call\)\>'
return 1
return 0

Suggestion : 2

I just figured it out. In my augroup, I was using a capital "P" in Python, when it should be lowercase. This following works perfectly:

augroup python_files
   autocmd FileType python setlocal noexpandtab
autocmd FileType python set tabstop = 4
autocmd FileType python set shiftwidth = 4
augroup END

Suggestion : 3

You have most likely mixed tabs and spaces in your python file, you must use only one of them.,In order to find the problem you can use the command :set list to have vim list all non-printable characters and see where the problem is. Tabs will be shown as ^I spaces will be shown as normal., The error says it all actually, you're mixing tabs and spaces. Double check that you only use one of the two (4 spaces is preferred) for indentation. – Timo May 16, 2018 at 7:49 ,It is a problem related to the vim editor I guess because I set up an Ubuntu server and installed vim very recently. How can I fix the indentation error?

A sample of my python file is

def __init__(self, csvFile, chunksize = 10000):
   self.newName = csvFile[: -4]
import ipdb;
self.csvFile = csvFile
self.chunksize = int(chunksize)
self.headers_without_timestamp = header_without_timestamp

self.total_rows = 0
self.username = username
self.password = password
self.dbname = "data"

I have the little error, but I can't fix it. When I ran the command python3 ~/home/Data/DCIX_OB.csv 1000 7, I got

  File "", line 37
  import ipdb;
  ipdb.set_trace() ^
     TabError: inconsistent use of tabs and spaces in indentation