views:

166

answers:

2

Hi. 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.

+1  A: 

A regular expression is what you're looking for:

Type / to enter in "search mode" and type:

\[Capítulo.*\]/
CMS
then you need to turn on highlighting :set hls
Mykola Golubyev
+5  A: 

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

Paul Ivanov
Another option for avoiding the closing bracket is to use a non-greedy match: /\[Capítulo.\{-}\]/. \{-} is Vim's equivalent to Perl's *? operator and says match zero or more, as few as possible.
Al