I have created this tab-separated file with Vim:

#id    #comma separated values
0   d1,d2,cu
1   iii vvv,iii ccc,mmm ccc

The function I use to read the file:

def read_file(filename):
    f = open(filename, 'r')
    raw = f.readlines()
    lines = [e.strip().split('\t') for e in raw if not e.startswith('#')]
    print lines
    out = dict()
    for line in lines:
        out[line[0]] = line[1].split(',') 
    return out

I get the following output:

[['0   d1,d2,cu'], ['1   iii vvv,iii ccc,mmm ccc']]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in read_file
IndexError: list index out of range

Even though I am using e.strip().split('\t') in the function, I get [['0 d1,d2,cu'], ['1 iii vvv,iii ccc,mmm ccc']] as output instead of [['0','d1,d2,cu'], ['1','iii vvv,iii ccc,mmm ccc']]

My .vimrc file contains the following:

syntax on
colorscheme default
set incsearch
set hlsearch
set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

Does it mean that tabs in Vim are spaces ? How to make tabs in Vim be the same as \t in python ?

When I edit the file in Gedit, the python function works as expected.

2 Answers 2


Does it mean that tabs in Vim are spaces ?

Yes, that is what expandtab setting does. See :h expandtab

How to make tabs in Vim be the same as \t in python ?

Change expandtab to noexpandtab. If you want to keep expandtab setting for most of the time, and just in that particular scenario have real tabs instead of spaces, than you can try to add autocommand based on a file type, or add a modeline:

# vim: set noexapndtab:

in those files.

Eventually, from :h 'expandtab':

To insert a real tab when 'expandtab' is on, use CTRL-V<Tab>.

I've added the following to my .vimrc to add the proper PEP 8 indentation:

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

This setting give me the standard four spaces when I hit tab. It also ensure the line length doesn’t go beyond 80 characters and the file will be stored in a Unix format. To insert space characters whenever the tab key is pressed I've used the set expandtab.

  • 1
    I would recommend to add this settings to the file $HOME/.vim/after/ftplugin/python.vim (see point 3 of :help ftplugin-overrule) or at least use autocmd FileType python ....
    – Ralf
    Apr 4, 2019 at 6:18
  • I agree with @ ralf: this should be a filetype specific configuration. Also for your answer to be more useful to other users it would be nice to explain what the different settings do and why you choose to combine them like this.
    – statox
    Apr 4, 2019 at 7:05
  • Thx for the feedback guys! Apr 4, 2019 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.