Vim: remove expandtab from ftplugin/python.vim

Created on 19 Aug 2016  路  4Comments  路  Source: vim/vim

from README.txt:

The default filetype plugin files contain settings that 95% of the users will
want to use. They do not contain personal preferences, like the value of
'shiftwidth'.

Yet in python.vim is:

setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8

Even though I prefer spaces some projects I work with use tabs (don't ask). I use per project .vimrc file for these with set noexpandtab which is being overwritten by values set in python.vim.

Is it possible to remove this line and let user choose his preferable settings or is there any less intrusive solution (if I want to keep using filetype plugin)?

Most helpful comment

Hi @rkuska , add following line into your vimrc could disable the default setting.

let g:python_recommended_style = 0

All 4 comments

I really don't care either way, and I can understand your argument against this, but perhaps you don't know the _reason_ these settings are there.

It's to comply with PEP8 guidelines for tabs vs. spaces. These guidelines are in place for several reasons, but in my opinion the most important is that Python itself sees tabs as 8 spaces, so if an individual sets tabstop to 4 or even 2, the code they see can have different meaning than the code Python actually executes.

In most languages indentation doesn't matter so people are free to use whatever they like, but in Python the amount of indent has semantic meaning. Mixing tabs and spaces in Python, with tabstop set to anything besides 8, is just asking for trouble. So, Vim's default settings prevent that.

It's pretty easy to override these settings in the .vimrc with a FileType autocmd, or using the after/ftplugin directory. Though, I'd certainly support an option baked into the filetype plugin.

@rkuska As @fritzophrenic correctly explained the settings are set to conform with what the vast majority of Python developers use and need. Changing that setting would affect too many people in a surprising way. You can set your personal preferences easily like this in your .vimrc:

" File type dependent options
if has("autocmd")
        " Enable file type detection, plugins, and indentation rules.
        filetype plugin indent on
        " ... other file types set here ...
        " Python indent.
        autocmd FileType python setl sw=4 sts=4 et
        " ... other file types set here ...
        " When editing a file, always jump to the last known cursor position.
        autocmd BufReadPost *
                \ if line("'\"") > 0 && line("'\"") <= line("$") |
                \       execute "normal g`\"" |
                \ endif
endif

You can put multiple file types on the same line if the settings are the same. For example:

        " C++, Groovy, Java, JavaScript, Perl, Python and Tcl indent.
        autocmd FileType cpp,groovy,java,javascript,perl,python,tcl
                \ setl sw=4 sts=4 et

Thank you both for your answers. I will try to use some of the proposed solutions. :-)

Hi @rkuska , add following line into your vimrc could disable the default setting.

let g:python_recommended_style = 0
Was this page helpful?
0 / 5 - 0 ratings