Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: need help with a tiny script code to highlight

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.

like image 634
brsunli Avatar asked Dec 29 '25 07:12

brsunli


1 Answers

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

like image 181
Paul Ivanov Avatar answered Jan 01 '26 19:01

Paul Ivanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!