Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim, right way to indent css and js inside html

Couldn't find proper solution in old questions, so

    <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <style type="text/css">
body, input{
    background-color:red;
}
        </style>
        <script>
function test() {
    return false
}
        </script>
    </head>
    <body>
        <div></div>
    </body>
</html>

everything except code inside <style> and <script> tags indented ok, how can I fix it?

like image 556
Kaign Avatar asked Jan 23 '16 18:01

Kaign


2 Answers

As it turns out: That this has been done on purpose! Even my current Vim 8.1 installation contains an indent/html.vim-file which has such zero-indentation as its default setting.

That is however configurable via vimrc with:

let g:html_indent_script1 = "inc" 
let g:html_indent_style1 = "inc" 

...and -shame on us- is also mentioned in :help html-indent

like image 180
MaxC Avatar answered Oct 21 '22 22:10

MaxC


I use othree/html5.vim plugin which supports css/javascript inside html. It works although this isn't probably the simplest solution.

Your code is indented like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <style type="text/css">
      body, input{
        background-color:red;
      }
    </style>
    <script>
      function test() {
        return false
      }
    </script>
  </head>
  <body>
    <div></div>
  </body>
</html>
like image 3
Yosh Avatar answered Oct 21 '22 21:10

Yosh