How do I fix incorrect inline Javascript indentation in Vim?

JavascriptVim

Javascript Problem Overview


I can't seem to get inline Javascript indenting properly in Vim. Consider the following:

  $(document).ready(function() {
    
  // Closing brace correctly indented
    $("input").focus(function() {
      $(this).closest("li").addClass("cur-focus");
    }); // <-- I had to manually unindent this

  // Closing brace incorrectly indented
    $("input").blur(function() {
      $(this).closest("li").removeClass("cur-focus");
      }); // <-- This is what it does by default. Argh!
  
  });

Vim seems to insist on automatically indenting the closing brace shown in the second case there. It does the same if I re-indent the whole file. How do I get it to automatically indent using the more standard JS indenting style seen in the first case?

Javascript Solutions


Solution 1 - Javascript

The most comprehensive and bug-free Javascript indentation script is the one by Preston Koprivica. The so called OOP script that is in the proposed answer has severe bugs, and does not indent code properly that has square brackets.

Solution 2 - Javascript

Use JavaScript Indent: Javascript indenter (HTML indent is included) by Preston Koprivica. Thanks for the heads-up from oligofren - give him an up-vote.

Solution 3 - Javascript

The scripts mentioned above do not format the closure-syntax often used in jQuery correctly:

$(function() {
  // only one level of indentation, not two
});

This script works better for me: http://www.vim.org/scripts/script.php?script_id=2765

Solution 4 - Javascript

Most of these answers are from 2009 and, frankly, are out of date.

vim-javascript is much more recent and up-to-date than Preston's script.

Installation is a bit more complicated if you haven't started using Vundle yet, but it doesn't seem to suffer from the issues of the alternatives.

Solution 5 - Javascript

maybe some combination of these settings should be in your VIMRC file.

syntax on 
set syn=auto 
set showmatch 
filetype on 
filetype plugin on 
filetype indent on 
set tabstop=4 
set softtabstop=4 
set shiftwidth=4 
set expandtab

Solution 6 - Javascript

I had this same issue. This is the best of all Javascript indentation scripts:

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

It requires the IndentAnything plugin

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

As an added bonus, I wrote this indent script that will make Javascript blocks quite pretty. It uses the default html indenter by default (and the IndentAnything one when within a Javascript block)

http://gist.github.com/371902

Solution 7 - Javascript

In case someone comes here please note the vim-javascript by pangloss at https://github.com/pangloss/vim-javascript helped me so far, i.e. Vim 7.4. And the above solutions from oligofren and Charles Roper didn't.

Solution 8 - Javascript

You don't have to install plugins specialised for Javascript, you can learn the built-in Vim options for indentation. Vim has quite a few options, and some of the indenting styles like cindent, smartindent and indentexpr have options of their own.

To check whether you are using cindent or smartindent or indentexpr, run:

:set cindent?
:set smartindent?
:set indentexpr?

Despite the name, cindent doesn't just apply to C programs, it can apply to a bunch of programming languages that share roughly the same syntax, including Javascript. Have a look at :help C-indenting for documentation about this. You can adjust the settings particularly with a line like this one, see :help 'cinoptions' and :help cinoptions-values. Here's an example configuration:

:au FileType js,javascript setlocal shiftwidth=2 softtabstop=2 cinoptions=j1,J1,(1s " see help cino-j cino-J cino-(

Solution 9 - Javascript

Assuming the syntax file has good indenting for java script, visually highlight the block and press =. This works for java so I would expect it to do something half decent for java script. The results probably also depend on the settings of tabstop, expandtab and maybe shiftwidth.

gq is useful too, it formats lines rather than indents them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCharles RoperView Question on Stackoverflow
Solution 1 - JavascriptoligofrenView Answer on Stackoverflow
Solution 2 - JavascriptCharles RoperView Answer on Stackoverflow
Solution 3 - JavascriptKristian HanekampView Answer on Stackoverflow
Solution 4 - JavascriptCory KleinView Answer on Stackoverflow
Solution 5 - JavascriptOldBuildingAndLoanView Answer on Stackoverflow
Solution 6 - JavascriptmikelikespieView Answer on Stackoverflow
Solution 7 - JavascriptArtyomView Answer on Stackoverflow
Solution 8 - JavascriptFlimmView Answer on Stackoverflow
Solution 9 - JavascriptPeterView Answer on Stackoverflow