Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?"

share|improve this question
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 '11 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. – Nathan Long Aug 31 '11 at 19:06
Now that I think about it, I suspect that BOTH the PHP and HTML rules are being applied. – Nathan Long Aug 31 '11 at 19:07

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .