Results 1 to 2 of 2
This is challenging for me because I am pretty new to Vim scripting, so what I want to do may very easy or it may be impossible, I don't know, ...
- 04-27-2011 #1Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
Vim Syntax Highlighting Challenge
This is challenging for me because I am pretty new to Vim scripting, so what I want to do may very easy or it may be impossible, I don't know, but here it is:
Vim has hundreds of syntax highlighting definitions already, but is it possible to selectively apply an existing highlighting scheme to only a precise portion of the text? Yes, of course, you can use the "syntax include" command, and you can select a region of text using "syntax region", and apply the highlighting to that region. But as far as I can tell, highlighting regions must be continuous in the text. So something like this would be impossible without completely redefining an existing syntax highlighting definition:Can this be done without redefining the entire Bash syntax definition? I could modify the existing Bash syntax to make it do what I want, but that would be quite difficult because the syntax definition is huge. I was hoping there was a way to perhaps create single syntax region out of the union of fragments of other regions, then highlight the text contained in that union region with a syntax highlighting group that has been included from a separate syntax file. That, or filter the bash text using a program like sed, then apply highlighting only to the portion selected by sed, leaving everything else to the default highlighting, as in this pseudo-code:Code:This is plain text in an e-mail... Hello, here is some code I want to show you! | #!/bin/bash | Plaintext goes here too: | export FOOBAR="/path/to/foo/bar" | Hey man, I translated part | sed -e 's/foobar/FUBAR/gi' <$FOOBAR | of the $FOOBAR to German. | echo "Done" | Anything outside of the leading and trailing "|" characters is not highlighted, anything between leading/trailing "|" characters is highlighted as Bash script. The width of each line of Bash code is fixed to 60 characters. Any characters after column 60 are plain text.
I've been trying to make it work in Vim, and got it to ignore the lines that contain plain-text only, but I can't figure out how to make it work right on the lines that contain Bash code. It wants to color everything including the "|" characters and the plain text. How can you narrow the syntax region to contain discontinuous sections within lines.Code:sed -e 's/^[^|]*[|]//' -e 's/[|][^|]*$//' <text-region-from-vim | ordinary-bash-highlighting >back-to-vim
If anyone wants it, I can post some of the code I have now, but I thought this problem might be straight-forward enough that someone can suggest their own solution.Last edited by ramin.honary; 04-27-2011 at 12:19 PM.
- 04-28-2011 #2Just Joined!
- Join Date
- Nov 2004
- Posts
- 8
I don't know whether you need highlighting in vim only or you want to produce highlighted output... in the latter case, you may want to consider this GNU Source-highlight (the forum does not allow me to post an URL, but you can easily google for it)


Reply With Quote