Using set syntax=lang
we can override syntax highlighting of current buffer with lang
.
Suppose we're editing a Bash script and some string variables are actually HTML code. How to force vim to use HTML syntax highlighting only for part of the code (or limited to only that variable)?
Consider the following bash script:
#!/usr/bin/env bash
a=$(curl "google.com")
b=$(printf "
<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\"
content=\"width=device-width, initial-scale=1.0\">
<title>my title</title>
</head>
<body>
%s
</body>
</html>" \
"$result")
c='#include "stdio.h"
void main() {
int var=10;
return 0;
}
'
I want part a
to be highlighted as bash, part b
as HTML, and part c
and C code.
Is it possible to use some markers in vim DSL as indicators of highlighting portions? (like #start_of_cpp #end_of_cpp)
:syntax include
should be enough for this? Take a look at the default syntax file for Markdown for an example on how it highlights stuff inside code blocks in Python etc. I don't really have the time for a proper explanation/answer, but that should point you in the right direction to start with.