Consider the following vim syntax rules, which I am using to change the color of words surrounded by *.
syntax match boldme /\*.\{-1,}\*/
highlight boldme ctermfg=Red
For some reason, this rule only works if the word is at the beginning of a line, *hello* is red in the first line below but not the second line.
*hello* works
Another word and *hello* does not work.
How can I make syn match work in the middle of a line for the scenario above?
Update: This problem appears to be specific to using the literal * character as part of the match. The following match works fine for using _ instead.
syntax match boldme /_.\+_/
Thus the question is really, how do I force vim to treat a literal * character correctly in syn match?
try this:
syntax match boldme /\*.\+\*/
Update
I don't know how did you do the test, see this gif animation with vim -u NONE:
Related
I would like to highlight lines matching this regex RED:
syn match RedLine "^\*\*\* .* \*\*\*\n"
Then I'd like to highlight the following line BLUE no matter what text it contains.
I tried using \zs to match the following line's pattern like this:
syn match BlueLine "^\*\*\* .* \*\*\*\n\zs.*"
But that doesn't work (my understanding is that the read position has passed the portion of the match before \zs already).
So I tried the "look behind" atom like this:
syn match BlueLine "\(^\*\*\* .* \*\*\*\n\)\#50<=.*"
But that was way too slow even with the 50 byte limit.
How can I always match an entire line whenever the previous line matches a certain pattern?
e.g.
*** this line's RED since it's surrounded by pairs of 3 stars ***
this line's always BLUE because of the preceding line
You actually don't need to re-parse the first line to capture the second, following line (which indeed is highly inefficient). Vim has :help :syn-nextgroup, which directs the parser to continue parsing with preference to the specified group. The skipnl keyword skips over the newline in between. As you indiscriminately want to highlight the entire next line, a simple .* pattern will do. The only important detail for that rule is the contained keyword, so that this rule only matches triggered by the nextgroup=, but never on its own (which would color the entire buffer blue).
syn match RedLine "^\*\*\* .* \*\*\*$" skipnl nextgroup=BlueLine
syn match BlueLine ".*" contained
I am trying to write a syntax highlighter in VIM. How do you highlight a match within another match?
To find each match, I created two syn match lines, which work where the matches are separate.
syn match celString "^xpath=.\{-};" -> matches "xpath=.........;"
syn match celComment "\${.\{-}}" -> matches "${LIB_METADATA};"
The first line is pink for the xpath string and blue for the ${..} string.
The second line is pink for the xpath string, but the ${..} contained inside that string is ignored.
I've tried to change the order of the syn match lines, but that doesn't have any effect.
I'd appreciate your ideas.
By default, Vim only applies the syntax groups to text that hasn't yet been assigned a syntax. To specify that one group can contain other groups, use the contains=... attribute:
:syn match celString "^xpath=.\{-};" contains=celComment
The order of definition shouldn't matter here. See :help :syn-contains for more information.
I am attempting to write a syntax file for Vim.
One of the lines of code reads
syn match constant "\**\*"
while one of many other lines reads
syn keyword aiOperators up-build
The code for highlighting is the following:
hi constant gui=bold
hi aiOperators guifg=green
However, the result of the above is that only the following is highlighted:
The asterisks of every constant, but not the characters between them.
Characters up until the first hyphen of aiOperators.
What seems to be the issue?
The regular expression for your constant specifies a literal asterisk, zero or more times, followed by a literal asterisk. If you intend to match characters delimited by asterisks, you need something like \*\w\+\*: a literal asterisk, followed by one or more word characters, followed by a literal asterisk.
The :syn keyword only works for keyword characters; by default, the hyphen is not included, so the match stops there. If, for your filetype, the hyphen belongs to the set of keyword characters, use
:setlocal iskeyword+=-
This should not be placed into the syntax file itself, but into ~/.vim/ftplugin/myfiletype.vim. Otherwise, use :syn match.
I'm trying to figure out how to highlight a specific portion of a match in vim.
Given the following example rule (taken from the coffeescript syntax file source):
syn match coffeeExtendedOp /\%(\S\s*\)\#<=[+\-*/%&|\^=!<>?.]\+\|[-=]>\|--\|++\|:/ display
This regular expression matches various coffeescript operators. The operators are highlighted (in my vimrc) like this:
hi Operator guifg=#ff0000
For example, since coffeeExtendedOp is linked to coffeeOperator which is linked to Operator, in the above source file. This all works, but I'm wondering how to specifically highlight the ++ operator matched in the above syn match with a different color, say blue, within my vimrc (that is, without altering the original source file above). I'm simply wondering if this is possible.
EDIT: I think the rules are placed under a cluster, so perhaps that's why it's not affecting anything. Is there a way to access the rule within the cluster?
EDIT: Question was clarified.
Solution:
syn match plusplus /++/ contained containedin=coffeeExtendedOp display
hi plusplus guifg=#0000ff
The problem now is that this only works when I run them as commands in vim, but not when I put it in my vimrc file. Any ideas? Could it be that the stuff is hidden behind the cluster? But then why is it visible in vim through a command? I tried including the syntax file but it didn't seem to have any effect.
Looking at the coffee.vim you linked to it seems like the dot belongs to the coffeeDotAccess syntax item. So you can highlight it just by doing this:
:hi coffeeDotAccess ctermfg=blue
I'm going to guess a bit at what you need. (I don't speak Coffeescript and your sample regex is way too complicated for me to start reading at the moment).
Transparent syntax items
You could have a look at transparent syntax rules: (http://vimdoc.sourceforge.net/htmldoc/usr_44.html)
In a C language file you would like to highlight the () text after a "while"
differently from the () text after a "for". In both of these there can be
nested () items, which should be highlighted in the same way. You must make
sure the () highlighting stops at the matching ). This is one way to do this:
:syntax region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/
\ contains=cCondNest
:syntax region cFor matchgroup=cFor start=/for\s*(/ end=/)/
\ contains=cCondNest
:syntax region cCondNest start=/(/ end=/)/ contained transparent
Partial matches in regex
If you really just meant highlighting submatches, have a look at the the
\zs start match
\ze end match
In short,
:match Error /foo\zsbar\zered/
would highlight only 'bar' in 'foobarred'
I need a script code to highlight "[Capítulo" and "]" and everything between them. Thank you.
I want it to work everytime I open , for example, a .txt file. Just like code highlighting.
Here's an easy way to do it:
in vim, make sure syntax highlighting is on with :syn on
run the command :highlight to get a listing of all the highlight group names, and samples of what they look like. The Error group looks like it stands out well in my colorscheme, so I'll use that in my example (but you can use any of the other names, like Todo or Search)
:syntax match Error /\[Capítulo[^\]]*\]/
This pattern will keep you from greedily matching the largest chunk. Even though other people are suggesting you use the regular expression /\[Capítulo.*\]/ - it's probably not what you want, because it will match everything in between if there are two or more such patterns on a line.
For example /\[Capítulo.*\]/ will match this entire line:
[Capítulo foo] these words should not be highlighted [Capítulo bar]
The same example but with /\[Capítulo[^\]]*\]/ will only match stuff inside []:
[Capítulo foo] these words should not be highlighted [Capítulo bar]
With regular expressions, it's a common trick to make a group that matches everything but the character that you want to end your match, instead of using the .* which will match as many characters as it can. In this case, we make the group [^\]]* - which says "match everything except ]."
If this works the way you want it to, add the syntax match line without the ":" to your .vimrc
A regular expression is what you're looking for:
Type / to enter in "search mode" and type:
\[Capítulo.*\]/