What is the best way to make vim highlight ejs (http://embeddedjs.com/) files? Is it possible to set up html highlight for the file in general and javascript highlight to it's parts inside <% %>? Appreciate your help!
6 Answers
Credits goes to @inkedmn just wanted to point out that html binding works way better, therefore put this in your ~/.vimrc file:
au BufNewFile,BufRead *.ejs set filetype=html

- 3,181
- 3
- 23
- 36
Here's something I whipped up today (made some modifications to the eruby script). It requires the vim-javascript plugin to be installed.

- 3,344
- 2
- 26
- 35
-
4this is great, if you're like me you might just want to grab the: https://github.com/briancollins/vim-jst/blob/master/syntax/jst.vim file and include it in your $HOME/.vimrc file as: au BufNewFile,BufRead *.ejs so $HOME/.vim/jst.vim – todd Jun 07 '12 at 04:03
-
3Nice. I just installed Vundle and then added `Bundle 'briancollins/vim-jst'` to my `~/.vimrc`. – Jason Swett Jul 04 '12 at 14:04
-
Looks like the correct file association for `*.ejs` should be `au BufRead,BufNewFile *.ejs setf javascript.jsx` – thom_nic Sep 22 '15 at 16:36
I've had the best results downloading this syntax file directly into ~/.vim/syntax

- 86,427
- 15
- 75
- 107
-
2To use this file I had to add to my .vimrc: au BufNewFile,BufRead *.ejs set filetype=ejs – Ultrasaurus Jul 17 '14 at 19:54
For a solution that uses javascript and html syntax where appropriate (and not rely on any third-party javascript plugins) you need an ftdetect file which runs autocmd
when files with the .ejs
extension are loaded combined with an ejs syntax file.
If you're not concerned with how it works I've put a package together than you can grab from github here. If using Vundle just add this to your .vimrc:
Bundle 'nikvdp/ejs-syntax'
To do it yourself, create two files in your ~/.vim
folder:
An ftdetect file: ~/.vim/ftdetect/ejs.vim
:
autocmd BufNewFile,BufRead *.ejs set filetype=ejs
autocmd BufNewFile,BufRead *._ejs set filetype=ejs
function! s:DetectEjs()
if getline(1) =~ '^#!.*\<ejs\>'
set filetype=ejs
endif
endfunction
autocmd BufNewFile,BufRead * call s:DetectEjs()
And a syntax file (from user456584's answer) : ~/.vim/syntax/ejs.vim
runtime! syntax/html.vim
unlet b:current_syntax
" Include Java syntax
syn include @ejsJavaScript syntax/javascript.vim
syn region ejsScriptlet matchgroup=ejsTag start=/<%/ keepend end=/%>/ contains=@ejsJavaScript
syn region ejsExpr matchgroup=ejsTag start=/<%=/ keepend end=/%>/ contains=@ejsJavaScript
" Redefine htmlTag so that it can contain jspExpr
syn clear htmlTag
syn region htmlTag start=+<[^/%]+ end=+>+ contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent,htmlCssDefinition,@htmlPreproc,@htmlArgCluster,ejsExpr,javaScript
" syn keyword ejsPrint contained print
syn match javaScriptType /\<\zsvars\ze\./
syn match javaScriptSpecial /\<\zsexports\ze\./
syn match javaScriptFunction /\<\zsprint\ze(/
syn match javaScriptFunction /\<\zsinclude\ze(/
syn match javaScriptFunction /\<\zsincludeObject\ze(/
syn match javaScriptFunction /\<\zsfetch\ze(/
syn match javaScriptFunction /\<\zsfetchObject\ze(/
command -nargs=+ HiLink hi def link <args>
HiLink ejsTag htmlTag
delcommand HiLink
let b:current_syntax = "ejs"

- 89
- 1
- 3
If you want them to be highlighted like regular .js files, you could add this to your .vimrc:
au BufNewFile,BufRead *.ejs set filetype=js
Not 100% sure that's what you're after - hope it helps.

- 27,655
- 8
- 56
- 72
-
Well, changing it a bit to au BufRead,BufNewFile *.ejs set filetype=javascript I could get the javascript hightlighting, however, it's not really readable... That would be really great to have html highlighted as html and javascript - as javascript (if it's possible). – lyuba Jan 04 '11 at 20:32
-
3Apparently you can use multiple filetype for a single buffer. See here : http://stackoverflow.com/questions/2601403/multiple-file-types-in-vim/2604558#2604558 – Xavier T. Jan 05 '11 at 15:25
-
This works, thanks! However, is there probably a way to apply this command automatically to every file with .ejs extension? – lyuba Jan 06 '11 at 14:57
-
try this
cd /usr/share/vim/vim74/syntax #maybe vim64 or other
cp html.vim ejs.vim
vim ejs.vim
you can just edit html.vim
but I suggest you not...
then find
syn region javaScript start=+<script\_[^>]*>+ keepend end=+</script>+me=s-1` contains=@htmlJavaScript,htmlCssStyleComment,htmlScriptTag,@htmlPreproc
and write
syn region ejsScript start=+<%+ keepend end=+%>+ contains=@htmlJavaScript,htmlCssStyleComment,htmlScriptTag,@htmlPreproc
under that line.
find
HtmlHiLink javaScript Special
add
HtmlHiLink ejsScript Special
under it
add this line to your ~/.vimrc
au BufNewFile,BufRead *.ejs set filetype=ejs
now your ejs code will looks like js code... or you just want it looks like something else?
replase
HtmlHiLink ejsScript Special
by (for example)
hi def ejsScript term=bold cterm=bold gui=bold
in fact, in this example,the two line can live together...
it makes your code lovely~
you can read this to help you with your vim-syntax

- 63
- 1
- 6