Referring to answer from this link..

Adding two groups,

:highlight MyGroup1 ctermbg=green guibg=green
:highlight MyGroup2 ctermbg=blue guibg=blue

Then adding two words foo and bar into specific groups.

:call matchadd("MyGroup1", "foo")
:call matchadd("MyGroup2", "bar")

But how to make sure that the exact words foo is highlighted and not some conjunction word bigwordwithfoo.

1 Answer 1


Use the \< and \> anchors around your pattern, to match word boundaries before and after your words.

:call matchadd("MyGroup1", '\<foo\>')
:call matchadd("MyGroup2", '\<bar\>')

You need single quotes, to prevent Vim from using \ as an escape character.

See also:

  • :help /\<
  • :help matchadd() for details on how pattern is used and matched.
  • :help literal-string for how single quoted strings don't interpret backslashes as escape characters.
  • How to add pattern such that, highlight word foo only on those lines which contain other word kookoo Commented Nov 5, 2019 at 11:25
  • @RohanGhige You should ask that as a new question, that way you will get visibility on your question and possibly attract better answers. Please don't use comments for whole follow up questions, more like clarifications on the answer, if you didn't get it to work, etc...
    – filbranden
    Commented Nov 5, 2019 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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