For better or worse, PHP code often includes both HTML and Javascript. Getting Vim to indent it all correctly can be tricky.

My .vimrc specifies that a tab should always be two spaces:

set tabstop=2
set softtabstop=2
set shiftwidth=2

This indent file does a good job with indenting mixed PHP and HTML and uses 2 spaces for both, but for some reason, it indents embedded Javascript with 4 spaces. The code snippet below gets auto-indented as shown.

  if (false) {
    $foo = 'foo';
    <script type="text/javascript">
          if (false) {

              // Four spaces!?
              var foo = 'foo';

If I create a separate javascript file, it indents as it should, like so:

if (false) {
  var foo = 'foo';

I'm not sure why Vim indents the Javascript in the first example as it does. Is there a way to ask Vim "what syntax or indentation are you using on this specific line?"

  • That sounds right. The convention in Javascript is to use 4 spaces. Check your Javascript syntax file. It may or may not be delegating it to that.
    – digitxp
    Aug 31, 2011 at 15:58
  • @digitxp - I don't have a /syntax/javascript.vim currently. I do have a indent/javascript, but that just decides when to indent or unindent, not how many spaces to use for an indent. And, as I said, a separate JS file gets indented with my default of two spaces. Aug 31, 2011 at 19:06
  • Now that I think about it, I suspect that BOTH the PHP and HTML rules are being applied. Aug 31, 2011 at 19:07

1 Answer 1


To find out the syntax in use, you want to know the value of the variable b:current_syntax. Type the following Ex. command

:echo b:current_syntax

b:current_syntax says what syntax vim loaded for the file. If you set the syntax manually, such as with the command

:ownsyntax cpp

which sets the syntax to c++ syntax, then you want to know the value of w:current_syntax. w:current_syntax doesn't exist until you set it, and you cannot change b:current_syntax of a loaded file.

Look at the current_syntax entry in help for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.