3

Suppose I want to make all text that matches some regex persistently and automatically (and without affecting / searches) appear in a different color. How would I do this in vim 7.4?

  • You only need to search that regex, vim will highlight matched text for you. – cuonglm Aug 12 '14 at 7:20
  • @Gnouc I don't want to highlight the text, I want to change the color. – quant Aug 12 '14 at 7:21
  • Do you mean syntax highlighting or the text matching a / search? – Michael Homer Aug 12 '14 at 7:25
  • @MichaelHomer I want to change the color of text that matches a regex (so the text that could be simply highlighted with a / search) – quant Aug 12 '14 at 7:26
  • What do you mean by "change the color"? – Michael Homer Aug 12 '14 at 7:27
3

Depending on exactly what you want, this is either what mark.vim or general syntax highlighting do.

Mark allows creating some number of distinct groups of highlighted patterns or extents. Specifying a particular regular expression is done with Leaderr by default. There's also a :Mark command. You can set the colourscheme using :MarkPalette.

| improve this answer | |
  • mark.vim seems won't work with vim 7.4. – cuonglm Aug 12 '14 at 7:51
  • Works for me, but I can't comment on what else might get in the way. – Michael Homer Aug 12 '14 at 7:55
  • @Gnouc: mark.vim supports Vim 7.2, 7.3, and also 7.4. The version given on vim.org is the lowest supported one! – Ingo Karkat Aug 18 '14 at 11:12
1

You can do something like this in your .vimrc:

highlight Test ctermfg=red
autocmd BufWinEnter * match Test /Power/
autocmd InsertEnter * match Test /Power/
autocmd InsertLeave * match Test /Power/
autocmd BufWinLeave * call clearmatches()

Then when you open file, any text matches Power will be highlighted as red. This is a static way, you can write your own function to work with any pattern and color you want.

You can also use mark.vim but it is only tested with vim version 7.2 and lower.

| improve this answer | |
  • Doesn't this interfere with other searches? – Bernhard Aug 12 '14 at 8:03
  • Yes, It will interfere, since when it's only a simple approach. We need more works if we want it really done. – cuonglm Aug 12 '14 at 8:08
  • That was his most important requirement, but it can be solved by using 2match or 3match. – Bernhard Aug 12 '14 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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