2

This is not a very simple question.

I found that the "shiftwidth" for python files are set to 4, while I have these at the bottom of my .vimrc:

set tabstop=2                                                               
set shiftwidth=2                                                               
set softtabstop=2                                                              
set expandtab 

The shiftwidth are 2 for all other filetypes; but it's 4 only for python.

I want to know if it's possible to see where a variable (in my case, the shiftwidth) has been changed; if this is not possible when what are the possible locations that this variable is changed..? And is there any way go make the shiftwidth=2 universally... even for the python files? (Although it's also recommended of using 4 spaces for python, but I personally prefer 2)

songyy
  • 4,323
  • 6
  • 41
  • 63
  • 1
    I feel like you are looking for something like this: http://stackoverflow.com/questions/158968/changing-vim-indentation-behavior-by-file-type http://stackoverflow.com/questions/891805/how-do-i-set-up-different-tab-settings-for-different-languages-in-vim or http://stackoverflow.com/questions/2011589/vim-python-indentation-not-working – Peter Rincker Apr 02 '14 at 20:46

2 Answers2

6
:verbose set shiftwidth

tells you what's the current value and where it was defined. See :help :verbose.

Filetype plugins are sourced after the corresponding FileType event was triggered so they will override the options in your vimrc.


The dirty way to add filetype-specific settings to your config is to add something like this to your vimrc:

augroup Python
    autocmd!
    autocmd fileType python setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
augroup END

See :help :autocmd.


The clean way is to put your desired settings into a custom ftplugin.

The file:

~/.vim/after/ftplugin/python.vim

Its content:

setlocal tabstop=2
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal expandtab

Note the use of setlocal instead of set because you don't want those settings to be applied everywhere.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 3
    For non-binary options, like `'shiftwidth'`, it does not make any difference whether you do `:verbose set shiftwidth` or `:verbose set shiftwidth?`. I think it is a good habit to add the optional `?` because it _does_ make a difference for binary options. For example, `:verbose set expandtab` will set the `'expandtab'` option, without telling me anything about where it was previously set or reset. – benjifisher Apr 03 '14 at 00:10
1

This is probably being set by the built-in ftplugin file for Python. If you

:e $VIMRUNTIME/ftplugin/python.vim

and search for shiftwidth, you’ll probably find it set there to 4.

Ben Klein
  • 1,719
  • 3
  • 18
  • 41