9

I installed vundle yesterday and since I did, the tabwidth I configured in my vimrc is ignored and set back to 4 instead of 2.

I found out that the following line after the vundle paragraph is causing it:

filetype plugin indent on

My indentation is set up like this:

set noexpandtab " Make sure that every file uses real tabs, not spaces
set shiftround  " Round indent to multiple of 'shiftwidth'
set smartindent " Do smart indenting when starting a new line
set autoindent  " Copy indent from current line, over to the new line

" Set the tab width
let s:tabwidth=2
exec 'set tabstop='    .s:tabwidth
exec 'set shiftwidth=' .s:tabwidth
exec 'set softtabstop='.s:tabwidth

You can check my full vimrc here.

I tested the indentation problem using a python script (where indentation really matters).

I already tried changing filetype plugin indent on to filetype plugin on but that doesn't change anything. Only commenting out that line helps.
Now, the vundle install guide says, this line is required.

How do I fix this indent issue? Can I just ommit the filetype line or is it really mandatory to keep it in the vimrc?

Solution:

Thanks to @ChristianBrabandt and @romainl I now found a solution that can also reside in a single vimrc file:

filetype plugin indent on

[...]

set noexpandtab " Make sure that every file uses real tabs, not spaces
set shiftround  " Round indent to multiple of 'shiftwidth'
set autoindent  " Copy indent from current line, over to the new line

" Set the tab width
let s:tabwidth=2
au Filetype * let &l:tabstop = s:tabwidth
au Filetype * let &l:shiftwidth = s:tabwidth
au Filetype * let &l:softtabstop = s:tabwidth
  • Even though it doesn't answer you question, I tried with vim-plug instead of Vundle and it worked perfectly... – nobe4 Aug 27 '15 at 9:23
  • 2
    See the faq – Christian Brabandt Aug 27 '15 at 10:06
  • You could also use the new OptionSet autocommand to reset the shiftwidth and softtabstop setting – Christian Brabandt Aug 27 '15 at 13:30
10

First things first; the line below has absolutely nothing to do with Vundle or plugin management:

filetype plugin indent on

That command does three things:

  • enables filetype detection,
  • enables filetype-specific scripts (ftplugins),
  • enables filetype-specific indent scripts.

That line is there because some plugin managers have to make sure filetype detection is disabled before doing their magic and using Vim for programming would suck a lot harder without ftplugins and proper indentation. It is my opinion that they should simply deal with filetype detection internally but well…

Anyway, your problem is caused by over-possessive ftplugins that override your indentation settings with theirs. The python ftplugin is the most common culprit because it was decided not long ago that it should enforce PEP8.

The easy way out would be to avoid sourcing ftplugins altogether:

filetype indent on

but they usually come with useful stuff so that method is not really recommended.

The cleanest solution is to leave the filetype line in its "optimal" state:

filetype plugin indent on

and override their overrides with your own after/ftplugin/python.vim:

setlocal noexpandtab
setlocal shiftround
setlocal autoindent

let s:tabwidth=2
let &l:tabstop = s:tabwidth
let &l:shiftwidth = s:tabwidth
let &l:softtabstop = s:tabwidth

Notes:

  • I removed smartindent because it is not that smart to begin with and deprecated by filetype-specific indent scripts anyway.
  • I replaced your :execute commands with cleaner :let commands to avoid unnecessary concatenation.
  • 1
    If you set shiftwidth to zero and softtabstop to -1 it will follow the tabstop setting. – Christian Brabandt Aug 27 '15 at 9:57
  • thanks for your explanations. I'd like to maintain most of my settings in my vimrc because I want to sync them to multiple computers. I used your solution in my vimrc and used the faq link @ChristianBrabandt posted below my question and it works. I'll edit my question to include my solution. – wullxz Aug 27 '15 at 12:11
4

The indentation problem comes from the ftplugin, which loads a .vim file from /usr/share/vim/vim-version-/ftplugin/-filetype-.vim that overrides whatever you have in your .vimrc file. you can find out where that file is by running the following command inside vim :verbose set tabstop?. The output will point you to the file that is overriding your configuration.

In my case I was having problems with my python indentation configuration.

An easy way to solve this is to do the following:

Create a .vim folder in your home folder (if you don't have it)

cd ~/.vim
mkdir -p after/ftplugin/
cd ~/.vim/after/ftplugin/
vim python.vim

Add the following:

setlocal noexpandtab shiftwidth=4 softtabstop=4 tabstop=4

Modify whatever you want in the command. Mine looks like that because I work with tabs instead of whitespaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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