joeforker
joeforker

Reputation: 41817

How do I make vim indent JavaScript in HTML?

Vim 7.0.237 is driving me nuts with indentexpr=HtmlIndentGet(v:lnum). When I edit JavaScript in a <script> tag indented to match the surrounding html and press enter, it moves the previous line to column 0. When I autoindent the whole file the script moves back to the right.

Where is vim's non-annoying JavaScript-in-HTML/XHTML indent?

Upvotes: 6

Views: 8145

Answers (5)

Seth
Seth

Reputation: 6842

I have plugins for indenting HTML and JavaScript files. To indent JavaScript inside HTML, I temporarily change the file type, select and indent the lines, and then change the file type back.

:filetype javascript
(select lines)
=
:filetype html

It's a little tedious, but it always produces the results I expect.

Upvotes: 0

Cory Klein
Cory Klein

Reputation: 55900

I recommend installing vim-javascript.

It is an up-to-date plugin that properly indents javascript, including more recent developments like the syntax used in closures such as with jQuery.

Upvotes: 1

Grzegorz Gierlik
Grzegorz Gierlik

Reputation: 11232

Here is similar question with accepted answer with links to two vim plugins:

  1. html improved indentation : A better indentation for HTML and embedded javascript mentioned by Manni.
  2. OOP javascript indentation : This indentation script for OOP javascript (especially for EXTJS) .

One of them solved my problems with JavaScript scripts indention problems.

Upvotes: 5

dlamblin
dlamblin

Reputation: 45381

Personally I toggle between :set ai and :set noai, but might be too tedious for you.

Upvotes: 0

innaM
innaM

Reputation: 47899

Have you tried this plugin?

Upvotes: 1

Related Questions