How do I make vim indent JavaScript in HTML?
-
05-07-2019 - |
Question
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?
Solution
Have you tried this plugin?
OTHER TIPS
Here is similar question with accepted answer with links to two vim plugins:
- html improved indentation : A better indentation for HTML and embedded javascript mentioned by Manni.
- OOP javascript indentation : This indentation script for OOP javascript (especially for EXTJS) .
One of them solved my problems with JavaScript scripts indention problems.
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.
Personally I toggle between :set ai
and :set noai
, but might be too tedious for you.
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.