6

I don't know if this is possible/sensible, but I was curious to know if I can have my strings in javascript files have html highlighting. I discovered that strings in php could have SQL syntax highlighting, so I believe it is possible.

But, I don't know vim-scripting, so any help on this appreciated.

I am using the Better Javascript syntax.

PS: If there could be an option to turn it on and off while editing a js file, that would be wonderful

Thanks

2
  • +1, I'd like to be able to do this too... I have a ton of HTML embedded in JS files. Python embedded in my vimrc is highlighted correctly, so this sort of thing is definitely possible. Commented Sep 25, 2009 at 4:49
  • wow! It really does.. I never wrote any python in my vimrc, so never noticed.. lol! Commented Sep 25, 2009 at 4:54

1 Answer 1

4

Yes, it's possible if you don't mind some syntax file hacking. First you need to include the HTML syntax file from within the Javascript syntax file -- see :help syn-include for info on that; second you need to declare that HTML syntax can be found inside of certain elements (i.e. strings). Third, if you want to have the option of enabling and disabling it, you can make those commands dependent on a global variable, and write some mappings that set or unset the variable and then reload the syntax file.

For examples on how inclusion works, take a look at syntax/html.vim (which includes the Javascript and CSS syntax files), syntax/perl.vim (which includes the POD syntax file), or php.vim (which includes SQL syntax highlighting in strings, conditional on a global ariable).

Edit: did some work on actually making this happen in my copy.

In the head of syntax/javascript.vim, just below syn case ignore, add

syn include @javaScriptHTML syntax/html.vim
unlet b:current_syntax
syn spell default " HTML enables spell-checking globally, turn it off

Then add @javaScriptHTML to the contained= lists for javaScriptStringD and javaScriptStringS.

Finally you have to edit syntax/html.vim to prevent it from trying to include syntax/javascript.vim if it was loaded from javascript: find the line that reads

if main_syntax != 'java' || exists("java_javascript")

and change it to

if main_syntax != 'javascript' && ( main_syntax != 'java' || exists("java_javascript")
Sign up to request clarification or add additional context in comments.

3 Comments

so, is it going to be like 'syntax include @javaScriptStringS syntax/html.vim', but if I add that to my javascript.vim, I am not able to see anything when I open a js file... what am I doing wrong?
somehow i still ended up getting html.vim imported recursively :(
Thanks hobbs, sorry that I am coming back quite late, but I tried your solution and I did manage to get some html colors in my strings, but there still are 2 problems, all the '>' symbols in the file appear with a red background, like unmatched or something, and the string first opened in the js file, never ends... i.e., the whole file after the first ' is highlighted as a js string... Thanks for the help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.