0

Is there any way to replace Vim's syntax highlighting?

For example, I now have this test script to make everything between quotes red:

#!/usr/bin/env python
import re, sys
print(re.sub(r'"(.*?)"', r'\033[91m"\1"\033[0m', sys.stdin.read()))

Can I use this in Vim instead of Vim's built-in syntax option? (Note that this script is just an example to show my idea). Or is there another way to do this?

  • 1
    This question is very unclear. What is a "syntax highlighting library"? Do you have a specific library you want to use? And how do you want to "integrate" with it (ie. what do you wnat it to do)? As far as I know, ctags has nothing to do with syntax highlighting... – Martin Tournoij Feb 19 '15 at 14:01
  • @Carpetsmoker You have right, I seriously misunderstood something! I repaired my question, maybe it were enough to a VtC removal? – peterh Feb 19 '15 at 14:05
  • You're still not really describing what you want to do. What does Vim do now? What do you want it to do? – Martin Tournoij Feb 19 '15 at 14:48
  • @Carpetsmoker I extended my question with the answer, maybe it is now understandable? – peterh Feb 19 '15 at 14:57
  • 2
    I've edited the question to what I think you mean... It's still not entirly clear why you would want to do this btw (XY-problem?) – Martin Tournoij Feb 19 '15 at 15:06
1

If you don't like the stock syntax rules that come in, you are free to override them with your own. However, at some level, these rules will have to be defined via Viml.

:help syntax will tell you all of the rules and conventions for how to do this, but you may wish to copy the existing syntax and modify it. If you run :scriptnames, you can see a list of all files currently loaded and find the path to the syntax file for the language you're interested in.

If you truly object to using VimL to define syntax, you could always create a python wrapper that has the interface you desire and calls :syn match/region/keyword under the hood. However, I'm hard-pressed to think of a way to simplify things enough to warrant the additional level of abstraction.

I find vim's syntax to be more powerful and clean than any python code I've seen that tries to accomplish the same. For instance, to define a string as something surrounded in double quotes that can contain \\ or \", you would say something like:

syn region String start=/"/ end=/"/ skip=/\\\\\|\\"/

Having to backslash-escape \ and | is a little unfortunate I suppose, but skip is just one of may features that make defining a syntax element easier than trying to cram it all in one regex.

A note on "make a string red"-- you have to do this in two steps in Vim:

  1. Say everything in this regex is called a string (typically in a syntax file)
  2. Say that a string is red (typically in a colorscheme file)

This is intentionally done so that you can use any colorscheme with any syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.