Syntax highlighting of multiple languages in a single program

78 views
Skip to first unread message

J S

unread,
Apr 9, 2021, 4:07:48 PM4/9/21
to Vim Mailing List
Check out this URL:

https://vim.fandom.com/wiki/Different_syntax_highlighting_within_regions_of_a_file

My overarching goal is to be able to write a shell script (bash) with embedded code in other languages (Perl, AWK, sed, Python, Expect, whatever) and have the main script syntax highlighted as shell (of course), but have the regions containing code in other languages highlighted in those other languages.

Specifically, right now, I am working on a shell script with embedded Expect. Something like

expect -c '
# Expect script here
'

At the above link, there is a solution that sort of works. It's really quite frustrating, because it will work one minute then not the next and so on. I can't find any rhyme or reason to it.

So, if anyone wants to check it out and see what's wrong with it (note, BTW, that the comments section there indicate the same sort of frustration as I'm seeing - people complaining that it works, then it doesn't and so on), that'd be great. Also, please suggest alternatives. Thanks.

Here is the function they provide at the URL:

" Found at:
" https://vim.fandom.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
function! TextEnableCodeSnip(filetype,start,end,textSnipHl) abort
let ft=toupper(a:filetype)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
endif
execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ matchgroup='.a:textSnipHl.'
\ keepend
\ start="'.a:start.'" end="'.a:end.'"
\ contains=@'.group
endfunction

After sourcing the above file, I do:

call TextEnableCodeSnip('expect','@begin=expect@','@end=expect@','SpecialComment')

Then I start typing (starting with the @begin...@ thing) and sometimes it works, sometimes it doesn't.

Also, the original instructions at the URL, have a line in there about :hi link
which I can't tell what it is there for or if it is needed. Sometimes, it works without it, sometimes not.

J S

unread,
Apr 9, 2021, 4:10:33 PM4/9/21
to Vim Mailing List
Forgot to mention in the original post.

I am testing this wtih VIM 7.4

rwmit...@gmail.com

unread,
Apr 9, 2021, 7:12:01 PM4/9/21
to vim_use
On Friday, April 9, 2021 at 4:10:33 PM UTC-4 joesc...@yahoo.com wrote:
Forgot to mention in the original post.

I am testing this wtih VIM 7.4


Why not try updating to something recent?   8.2 is current.

Johannes Köhler

unread,
Apr 11, 2021, 4:07:54 AM4/11/21
to vim...@googlegroups.com
On 09.04.21 22:07, 'J S' via vim_use wrote:

> https://vim.fandom.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
>
> At the above link, there is a solution that sort of works. It's really quite frustrating > because it will work one minute then not the next and so on. I can't
> find any rhyme or reason to it.

Hi J S,

iam not familiar with the use of different language source, in the same
file / buffer. But my suggs are (a maybe case):

1) U can use modeline(s) to define different syntax indent on different
buffer lines:
https://vim.fandom.com/wiki/Modeline_magic
vim :help auto-setting

2) Youre problem could be (with this script) to set the right
vim :help cpoptions, also. I would try plugin syntaxrange mentioned
in the wiki help

thx sincerely
- kefko
Reply all
Reply to author
Forward
0 new messages