1

I'm trying to learn to use Vim. I like indenting by 4 spaces but use 2 spaces for certain languages, such as Nim and MoonScript. I tried adding this to my .vimrc:

autocmd BufNewFile,BufRead,BufEnter *.nim :setlocal tabstop=2 shiftwidth=2 softtabstop=2

Problem? It doesn't do anything! All that happens is that the tab does nothing when I press it! What am I doing wrong?

4
  • 2
    Have you tried doing it in a ftplugin? Commented Jun 2, 2015 at 18:01
  • @lcd047 What do you mean? Commented Jun 2, 2015 at 18:26
  • 1
    Something like this: echo setlocal tabstop=2 shiftwidth=2 softtabstop=2 >>~/.vim/ftplugin/nim.vim Commented Jun 2, 2015 at 18:31
  • 1
    Check :help filetype-plugin. If your tab does nothing it is probable that you had something mapped to it - you could try :imap <tab>. Commented Jun 2, 2015 at 18:46

1 Answer 1

2
autocmd BufNewFile,BufRead *.nim setlocal tabstop=2 shiftwidth=2 softtabstop=2

This should work (after restarting Vim / re-editing an existing file via :e!) fine, but it mixes filetype detection with filetype settings. Vim has an intermediate abstraction called filetype, which you should use. With it, you map file globs like *.nim to a filetype nim, and then define settings either via an :autocmd FileType, or a filetype plugin in ~/.vim/ftplugin/nim.vim (for the latter, you need :filetype plugin on in your ~/.vimrc).

Steps

So, create a filetype detection in ~/.vim/ftdetect/nim.vim:

autocmd BufRead,BufNewFile *.nim setfiletype nim

Then, create a filetype plugin in ~/.vim/ftplugin/nim.vim:

setlocal tabstop=2 shiftwidth=2 softtabstop=2

You can check that these are loaded correctly via the :scriptnames output. After a restart of Vim, this should work, and you can add additional settings to the latter file. If your filetype is derived from another, you can also add :runtime! ftplugin/javascript.vim (for example) in there to get those settings, too.

Sign up to request clarification or add additional context in comments.

Comments

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.