3

This question already has an answer here:

I have two match lines in my vimrc. they work when they are used by themselves but when i try to uncomment both i don't get any matching behavior:

" highlight spaces used for indenting
match Visual /^\t*\zs \+/
" highlight overlength characters
match Visual /\%81v.\+/

How do you get both match "indenting spaces" and match "characters past 80 column" to match at the same time?

Rewriting the original question to be more generic: How do you get two matches at the same time to work?

p.s. If you are wondering why I have these specific matches. The matches are used to help enforce the correct indentation style (the above matches are for tab-indentation and I have some matches for spaces-indentation).

marked as duplicate by Rich, Herb Wolfe, Mass, Community Apr 9 '18 at 19:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I experimented with changing the order of the match commands... that's when i discovered that the match command only enables the most recent match but the documentation does not seem very clear on that caveat/behavior... at least to me. – Trevor Boyd Smith Oct 12 '17 at 13:53
  • The other misleading factor for me at least is that the article I read talking about match does not mention anything about the fact that you can only have one match active at a time (and further matches require 2match and so on). – Trevor Boyd Smith Oct 12 '17 at 13:58
5

Use 2match for your second match.

" highlight spaces used for indenting
match Visual /^\t*\zs \+/
" highlight overlength characters
2match Visual /\%81v.\+/

There's also :3match, but this is used by the pre-installed matchparen standard plugin (see :help :3match), so if you need more matches than this, you're probably best off using the matchadd() function, or using syntax highlighting instead.

Alternatively, if you're using the same highlighting group, as you are, you can combine your regular expressions with \|:

" highlight spaces used for indenting and overlength characters
match Visual /^\t*\zs \+\|\%20v.\+/
  • 3match is supposed to be only used by the matchit plugin. You can also combine your various regula expressions into a single one, e.g. \(re1\)\|\(re2\) – Christian Brabandt Oct 12 '17 at 13:56
  • @ChristianBrabandt I was just in the process of updating my answer regarding the matchparen plugin. I'll add your note about combining regular expressions, though. – Rich Oct 12 '17 at 14:00
  • @ChristianBrabandt I am aware that you can combine multiple patterns in one match using regex syntax. However doing so is less elegant because I very strongly prefer to have a comment line pre-ceding each match to describe the function/purpose of the match command. So if I ever needed more than 3 I would probably go the extra mile and figure out the syntax required to call matchadd() function. – Trevor Boyd Smith Oct 12 '17 at 14:06
  • if i have 3 or more matches, should I not use 3match because it is reserved for use by matchparen (so it's off limits)? should I instead use the matchadd() function? – Trevor Boyd Smith Oct 12 '17 at 14:09
  • 1
    I would always go with the matchadd() functions, they are easier to use and handle (cleaning up etc) – Christian Brabandt Oct 12 '17 at 16:11

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