Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript indentation in VIM

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.

like image 537
Brian M. Hunt Avatar asked Jul 11 '10 16:07

Brian M. Hunt


3 Answers

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
like image 124
Dean Avatar answered Nov 16 '22 07:11

Dean


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
like image 25
Frederico Cabral Avatar answered Nov 16 '22 08:11

Frederico Cabral


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.

like image 4
sleepynate Avatar answered Nov 16 '22 08:11

sleepynate