Charles Roper
Charles Roper

Reputation: 20625

How do I fix incorrect inline Javascript indentation in Vim?

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?

Upvotes: 65

Views: 28125

Answers (9)

Flimm
Flimm

Reputation: 151298

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-(

Upvotes: 2

Artyom
Artyom

Reputation: 3571

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.

Upvotes: 2

Cory Klein
Cory Klein

Reputation: 55870

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.

Upvotes: 11

oligofren
oligofren

Reputation: 22993

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.

Upvotes: 88

mikelikespie
mikelikespie

Reputation: 5842

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

Upvotes: 3

Kristian Hanekamp
Kristian Hanekamp

Reputation: 2489

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

Upvotes: 17

Charles Roper
Charles Roper

Reputation: 20625

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

Upvotes: 83

Peter
Peter

Reputation:

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.

Upvotes: 0

OldBuildingAndLoan
OldBuildingAndLoan

Reputation: 3032

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

Upvotes: 7

Related Questions