2

I have the following vimrc configuration which is working fine for Python files:

execute pathogen#infect()
syntax on
filetype plugin indent on
set tabstop=4

So when I press tab it's an alias to 4 spaces.

But it doesnt seem to work with JavaScript files.

When I press tab it always add a tab plus 2 spaces for every indentation. I am ok with the two spaces but how can I replace that tab with 4 spaces?

All the code is indented with spaces I dont want start adding tabs/spaces mix

I know you can specify the tabs/spaces for specific files but I cant make it work

autocmd FileType javascript setlocal shiftwidth=1 tabstop=4
1
  • 1
    Just a tangent comment: if you want to fix your existing files, the tools expand and unexpand might come in handy. Hope this helps! Commented Sep 13, 2017 at 20:15

1 Answer 1

5

To configure 4-space indentation, you need to :setlocal tabstop=4 expandtab. To be safe, it's recommended to also reset the 'softtabstop' option to 0. Usually, you want the 'shiftwidth' option aligned to the chosen indent; that would be shiftwidth=4:

autocmd FileType javascript setlocal shiftwidth=4 tabstop=4 softtabstop=0 expandtab

I would recommend putting the options into ~/.vim/after/ftplugin/javascript.vim instead of defining lots of :autocmd FileType javascript; it's cleaner and scales better; requires that you have :filetype plugin on, though.

If that doesn't work, some filetype plugin (or another :autocmd) may override your settings. You can check with

:verbose setlocal shiftwidth? tabstop? softtabstop? expandtab?
Sign up to request clarification or add additional context in comments.

4 Comments

mmm It doesnt work, and the :verbose says it has been defined in vimrc (where I put the autocmd to test it) However it seems to work if I add to the vimrc just set shiftwidth=4 and set expandtab after the configuration written on my question, maybe vim is not detecting the javascript file? or any vim pathogen stuff is overriding it?
Could be; you can check via :setlocal filetype?, which should return javascript.
yep, it says javascript, it's strange I cannot make it work with autocmd but it works if I just add the options separately at vimrc
Try switching from :autocmd to the file ~/.vim/after/ftplugin/javascript.vim; that gives you at least the precise location with :verbose, and maybe solves a problem with autocmd ordering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.