Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim changes shiftwidth for .sml files

Vim under Win 7 changes my shiftwidth setting to something I don't want for .sml files only.

What I want is everything to be indented with spaces only, and for everything to be 4 spaces always. This is the contents of my current _vimrc file, though I've tried many variations:

set nocompatible
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4

set guifont=Consolas:h9:cANSI
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

filetype plugin indent on

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

When I first start Vim with no file loaded, :set shiftwidth returns shiftwidth=4. When I open a .py or .php file, it still returns shiftwidth=4. But when I open an .sml file or just save the blank file with an .sml extension, it changes to shiftwidth=2. Manually setting :set shiftwidth=2 works for that session, but I have to do it every time I open an .sml file.

It only affects buffers with an open .sml file. Unsaved buffers or ones with another file type open at the same time show shiftwidth=4.

I wasn't using the filetype plugin indent thing before, but after some searching it seemed that might be the problem. There were no files in my ftplugin folder, so I added filetype plugin indent on to my _vimrc, created sml.vim and put it in the ftplugin folder (again, having tried many variations:

set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4

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

I know sml.vim is being read because I put errors in it at first, and they were caught.

Not sure if this is relevant, but there's nothing about .sml in my indent.vim, and my indent folder is empty.

Yet Vim still sets shiftwidth=2 every time I load an .sml file. I'd like to know why.

Update: The indent folder that I said was empty was vim\vimfiles\indent. After the answer and comments below I searched for sml.vim and found another indent folder, vim\vim73\indent. I edited sml.vim and changed shiftwidth=2 to shiftwidth=4 and the problem was solved.

like image 896
gotube Avatar asked Jan 18 '13 16:01

gotube


1 Answers

It is most likely due to some filetype plugin. If it was the default one (i.e. shipped with vim itself) the correct fix will be putting appropriate sets

setlocal ts< sw< sts< " Set ts, sw and sts to global values

^ into ~/.vim/after/ftplugin/sml.vim. But there is no such settings in vim distribution. In this case the above solution may work (depending on the place where this non-standard plugin is located), but it would be better to examine verb set sw first and check whether plugin that defined &sw is a) the one you really need or b) configurable so that sets can be disabled without using after/ directory.

Update: It appears it actually is default one, but indent script and not ftplugin as @Nikita Kouevda pointed out. Thus you need to put this line into ~/.vim/after/indent/sml.vim.


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

Use this instead:

let s:tabwidth=4
let &tabstop     = s:tabwidth
let &shiftwidth  = s:tabwidth
let &softtabstop = s:tabwidth

or

let [&tabstop, &shiftwidth, &softtabstop]=repeat([4], 3)
like image 105
ZyX Avatar answered Oct 05 '22 00:10

ZyX