16

I'm trying to get VIM to indent Javascript with the '=' and related commands. When I try to auto indent the following, for example:

   new function($) {
     $.fn.setCursorPosition = function(pos) { 
       if ($(this).setSelectionRange) {
         $(this).setSelectionRange(pos, pos);
       } else if ($(this).createTextRange) {
         var range = $(this).createTextRange();
         range.collapse(true);
         range.moveEnd('character', pos);
         range.moveStart('character', pos);
         range.select();
       }

The result is the rather absurd:

       new function($) {
       $.fn.setCursorPosition = function(pos) {
       if ($(this).setSelectionRange) {
       $(this).setSelectionRange(pos, pos);
       } else if ($(this).createTextRange) {
       var range = $(this).createTextRange();
       range.collapse(true);
       range.moveEnd('character', pos);
       range.moveStart('character', pos);
       range.select();
       }

I've set set syntax=javascript, and I've set filetype to:

filetype detection:ON  plugin:ON  indent:ON

Though I've tried every permutation of this. I've tried every permutation of smartindent, autoindent, and cindent, but nothing seems to have the correct effect of giving Vim the expected indentation. I've set tabstop=4.

I've installed javascript.vim, and IndentAnything, though they don't seem to have any effect.

I'd be very grateful for any suggestions as to how to get Vim indenting properly in JavaScript.

1
  • 1
    Side note: Installing javascript.vim and IndentAnything solved a problem for me where vim was only indenting some of the lines correctly. Commented Dec 7, 2012 at 18:28

8 Answers 8

12

Oh man, I just spent a couple of hours figuring out the same problem.

If you have filetype indent on (which you do), then a few different indent settings may be set by a file somewhere. If you use verbose set <option>? you can see where it's being set and what it's set to:

:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?

By default, you'd only expect to see cindent set by the default indent file:

cindent
Last set from $VIMRUNTIME/indent/javascript.vim

Where $VIMRUNTIME is the path you get when you run :echo $VIMRUNTIME.

All of the others wouldn't be set unless you enable them (in your vimrc or a plugin).

For me, I had a plugin (eclim) that was setting identexpr and causing this issue:

identexpr=EclimGetJavascriptIndent(V:lnum)
Last set from ~/.vim/bundle/eclim/indent/javascript.vim
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this! You helped me locate a buggy plugin as well. :)
Wow, thank you. You just helped me debug an inexplicably broken vim. It would not autoindent in JS files. It turned out to be a bundle at .vim/bundle/vim-jsx (which I just had to remove, since I just switched to github.com/othree/yajs.vim anyway). Thanks. I want to buy you a beer, if the opportunity ever presents itself.
5

I had the same problem some time ago and the solution was the plugin "vim-javascript". It is JavaScript bundle for vim providing syntax and indent plugins.

https://github.com/pangloss/vim-javascript

The installation is very simple.

If you are using pathogen, use the follow steps:

  cd ~/.vim/bundle
  git clone https://github.com/pangloss/vim-javascript.git

If you are using vundle use the follow steps:

Add the follow line to your vimrc file:

Plugin "pangloss/vim-javascript"

And install it:

:so ~/.vimrc
:PluginInstall

1 Comment

This plugin works really well. FWIW, I was having a problem despite having this plugin installed, and because another plugin (vim-polyglot) was superceding it and causing problems. So if anyone still has problems after installing this, spot-check your other plugins
4

Adding the two closing braces and selecting the entire block with vi{ provided proper automatic indentation for me in gvim 7.2 with no plugins. You may want to see if an errant plugin is messing it up by starting vim with the --noplugins flag on the command line. and try again.

Comments

3

I hate to say something unhelpful like "It works for me", but it does. Even with nothing in my .vimrc and all plugins off, I get the correct indentation.

new function($) {
    $.fn.setCursorPosition = function(pos) { 
        if ($(this).setSelectionRange) {
            $(this).setSelectionRange(pos, pos);
        } else if ($(this).createTextRange) {
            var range = $(this).createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }

Have you tried loading vim with the --noplugins switch and temporarily moving/renaming your .vimrc to see if it still doesn't work? I suspect another setting in your .vimrc or another plugin may be causing the conflict.

Comments

3

For me it works (not very helpful statement, I know ;-) ). I suppose that the filetype is not detected correctly.

What does

 :set filetype

say? It should report "javascript".

[EDIT] Hint: Please note that there is an option called 'filetype' and a command called :filetype. To get help for the option do :help 'filetype' for the command do :help :filetype.

6 Comments

@Habi: Thanks. It'd be a shame to have missed that, but alas :set filetype is javascript. :)
@Brian M. Hunt: Hmmm, maybe the indentation is 0 (I don't know if this is possible at all, but if it is possible it could be a reason for your problem). What does ":set shiftwidth" and ":set cinoptions" say?
@Habi: :set shiftwidth is shiftwidth=4 and set cinoptions is cinoptions=. hrmm.
@Brian M. Hunt: Same here. Strange. Does the '=' work for other languages, for example C?
@Brian M. Hunt: Another idea: What does ':set equalprg" say? Its value should be "" by default.
|
3

I was having problems the other day with MacVim 7.2 and a Lua file that wouldn't indent correctly -- even after using set syntax, set filetype and filetype indent on, it wasn't indenting the file correctly.

I discovered adding:

filetype plugin indent on

to my .gvimrc file solved the issue, at least for me. YMMV.

1 Comment

Thanks for the reply & glad you solved this. Unfortunately this didn't seem to help.
1

If you want to indent without using any plugin.

  • Switch to Normal by hitting ESC key.
  • Type gg=G

What is does is: gg moves the cursor to the top of the file, = will trigger auto-indentation, G will move the cursor to the end of the file.

Comments

0

For me the presence of more than one '(' character before the '{' character on the same line seemed to change the indentation algorithm used by '='. Removing one of the '(' seemed to fix '=' for all parts of the file except other scope regions '{...}' which also had multiple '(' on the opening line.

I'm using vim 7.4 in lubuntu

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.