Jitesh
Jitesh

Reputation: 325

Vim ignores shiftwidth specified in .vimrc

I am using Vim 7.3.154 cli on Linux Mint 12 "Lisa"(x64).

I prefer using a tabstop and shiftwidth of 2 columns. My .vimrc has
:set ts=2
:set sw=2
to accomplish the same.

Whenever I open a new instance of Vim, tabstop value is retained, so existing tabs are rendered according to .vimrc. But somehow the shiftwidth value gets changed to 3. This results in 3 column indention while auto-indenting.
Running :set sw=2 on the new instance of Vim fixes this issue.
Can anyone tell me why Vim is ignoring/changing shiftwidth value from .vimrc?

I tried disabling all plugins to no avail.

Vim compiled options | .vimrc

Upvotes: 11

Views: 7465

Answers (3)

Wouter
Wouter

Reputation: 3

I had the same issue. shiftwidth was 2 in de .vimrc but after opening vim it said 4. And this on centos and on mint with the same config it was ok.

After i saw the command "verbose set shiftwidth?" i learned the option was done in /usr/share/vim/vim80/ftplugin/python.vim sp apparently this file is loaded after .vimrc (strange). The issue was also only with .py extension, i didn't notice that before.

After editting this file it was ok. Best way would ben to change the sequence in which the configs are loaded i assume.

Upvotes: 0

silversn
silversn

Reputation: 21

You can run:

verbose set shiftwidth ? 

to check what is setting the value of shiftwidth. It might coming from a plugin. You can also choose to modify the value there.

Upvotes: 2

greduan
greduan

Reputation: 4938

This answer just summarizes what we discussed in the comments. :)

This is most likely caused by the fact that you have file specific settings enabled. Check :h modeline for more info on that. Make sure that those files you have the problems with don't have this line in them.


Instead of only setting tabstop and shiftwidth you should also setup a couple more settings regarding whitespace and tabs.

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=4
exec 'set tabstop='    .s:tabwidth
exec 'set shiftwidth=' .s:tabwidth
exec 'set softtabstop='.s:tabwidth

Check this video for some extra info: http://vimcasts.org/episodes/tabs-and-spaces/


So this is the answer to the actual problem that you were able to solve. That php.php file was in the /plugin directory. That directory gets loaded once, each and every time Vim starts up. Check this: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimplugin

If what you want is that file to load only on PHP files then you should put it in the /ftplugin folder: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimftplugin

Read the documentation there, it should be a .vim file, in other words in this case it would be called php.vim.

What Pathogen or Vundle do is modify the runtimepath (:h runtimepath), nothing more, nothing less.

So you can now accept this answer, by clicking the little green arrow to the left of this answer. :)

Upvotes: 6

Related Questions