10

I know this has been asked before, but I'm having trouble getting JavaScript indentation to work properly in Vim.

I tried installing this plugin:

http://www.vim.org/scripts/script.php?script_id=3081

And I get this behaviour:

if (x == 1) {
alert("nice");
}

This is my vimrc:

syntax on
set background=light
colorscheme solarized
set tabstop=4
filetype plugin indent on
let g:solarized_termcolors=16

I also tried it with this plugin:

http://www.vim.org/scripts/script.php?script_id=1840

But that gives me this:

if (x == 1) {
        alert("nice");
}

i.e., two tabs, where I only want it to indent by a single tab.

Anyone have any ideas what to do here?

3 Answers 3

20

Vim wiki explains how to setup filetype-specific indentation, and it's pretty straight-forward: http://vim.wikia.com/wiki/Indenting_source_code#Different_settings_for_different_file_types

The simplest way is to put autocmd FileType instructions in your .vimrc file. You can specify indentation for each file type separately:

autocmd FileType javascript setlocal shiftwidth=2 tabstop=2
autocmd FileType html       setlocal shiftwidth=2 tabstop=2
autocmd FileType python     setlocal shiftwidth=4 softtabstop=4 expandtab

or set default indentation for all file types, and override it for the specific ones:

set tabstop=4
set shiftwidth=4

autocmd FileType javascript setlocal shiftwidth=2 tabstop=2                                                   
autocmd FileType html setlocal shiftwidth=2 tabstop=2
Sign up to request clarification or add additional context in comments.

1 Comment

The problem for me is that JavaScript in HTML is broken. HTML correctly indents with 4 spaces, but the JavaScript indents with 16 spaces and I can't figure out why.
7

I came here from google and was unsatisfied with Yi Zhao's indent file as suggested above. Still wasn't catching some nested functions of mine.

I asked around on twitter and was suggested https://github.com/pangloss/vim-javascript - with which I am far happier.

HTH,

1 Comment

Thanks a lot! You're right... the pangloss one you suggested is better than the Yi Zhao and jelera alternatives.
4

Have you tried this in your .vimrc

set smarttab
set cindent

edit also the JavaScript "plugin" I use for VIM is javascript.vim which replaces the default VIM javascript syntax file.

No matter what plugins you use, indenting in VIM is usually pretty bad, and is a common complaint with VIM users, especially with JavaScript. There is no perfect solution, which is strange considering the powerful extensibility of VIM.

3 Comments

I work in Vim all day on JavaScript and don't seem to have any real issues with Javascript indenting. Have a look at my Vim dotfiles on Github --> https://github.com/shanestillwell/dotvim
Although this is marked as the right answer, stackoverflow.com/a/9896760/209864 worked better for me.
@ShaneStillwell those per-language Autocmds are right to the point.

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.