Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In most cases, I want to use tabstop=4 while editing files. But for some types of file, like xml file, I want to use tabstop=2.

I added

au filetypedetect FileType xml,html,xhtml,javascript set tabstop=2 softtabstop=2 shiftwidth=2

in my .vimrc file. It works if I am editing only one file. But if I am editing multiple files, it cannot set the tab stops correctly for all the buffers. It seems the file type is detected and set only on startup, and never change since. I want the FileType set each time I switch the buffer, how can I do that?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I am not sure why your version is not working, but I am able to do this using the call option and putting the settings in a function like this:

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

function! setAltPrefs()
    set tabstop=2
    set softtabstop=2
    set shiftwidth=2
endfunction

autocmd FileType xml,html,xhtml,javascript call setAltPrefs()

This should get fired any time a file is loaded into a buffer or the filetype changes. The only catch would be if you change a buffer from one of the custom types back to one that should just be defaults. In that case nothing will change. You would have to setup another function to trigger on all over file types if you need to cover that use case.

share|improve this answer
    
Thank you. For the issue you mentioned, you can use the setlocal instead of set. I just figured it out :) –  davidshen84 May 4 '11 at 1:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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