Skosh
Skosh

Reputation: 175

Vim doesnt respect vimrc indentation settings

I am new to using Vim, and have been using "The Ultimate Vim configuration" on GitHub. It uses 4 spaces wide tabs as default, so to change that, I changed my .vimrc file to the following:

set runtimepath+=~/.vim_runtime

source ~/.vim_runtime/vimrcs/basic.vim
source ~/.vim_runtime/vimrcs/filetypes.vim
source ~/.vim_runtime/vimrcs/plugins_config.vim
source ~/.vim_runtime/vimrcs/extended.vim

" Own settings
set number
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab

try
source ~/.vim_runtime/my_configs.vim
catch
endtry

The my_configs.vim file is deleted, so I know that the indentation settings are not being overwritten there.

The problem is, if i source the vimrc file from vim with :source ~/.vimrc The indentation works perfectly, but if I close Vim and open again, then the indentation is back to 4 spaces wide instead of 2 as specified in the vimrc... All other settings from the Ultimate Vim Configuration get loaded from vimrc but not the indentation settings, which is really frustrating, as I need to source the vimrc file every time i want to use vim.

Any help is greatly appreciated, I really would like to exclusively use Vim but if this cannot be solved then I need to keep using Visual Studio Code :(

Thank you in advance.

EDIT: Here is the output of running command :verbose set tabstop? softtabstop? shiftwidth?:

tabstop=2, Last set from ~/.vimrc line 10
softtabstop=2, Last set from ~/.vimrc line 11
shiftwidth=2, Last set from ~/.vimrc line 12

Upvotes: 0

Views: 747

Answers (1)

filbranden
filbranden

Reputation: 8898

When you're editing a Python file in Vim, it will auto-detect the file type and apply Python specific settings. Amongst those, it will set the soft tab stop recommended by PEP 8.

You can find these settings in ftplugin/python.vim in your Vim runtime:

if !exists("g:python_recommended_style") || g:python_recommended_style != 0
    " As suggested by PEP8.
    setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
endif

You can override those settings with a global variable, which you can set in your vimrc. To disable the Python tab stop automatically set by Vim, include the following somewhere in your vimrc:

let g:python_recommended_style = 0

Note that those settings are there for a reason, they're pretty standard for Python and it's highly unusual to see Python code indented by a different number of spaces... But you have the knob to override it if you really want to.

Upvotes: 2

Related Questions