tags:

views:

382

answers:

5

Is it possible to mark a range of text in Vim and change the highlight color of it (to red) than select another range of text and change that color (to green) keeping the previous highlight and so on?

A: 

I am not aware of any way of doing something like this since it would require storage of metadata not related to actual content of the file. Even if it's only in memory while Vim is running, I don't know of a way to do it.

Doesn't mean there isn't a way since my knowledge of Vim is limited. But it seems like something Vim wouldn't do.

EMPraptor
Agreed. Highlighting is applied to highlight groups, which are made by matching file content (e.g. syntax highlighting). There has to be something *in the file* for them to match. Vim is a text editor, not a word processor. That said, depending on exactly what you want, you could create some custom syntax highlighting and a basic markup language, if you wanted.
Jefromi
I thought that it is a problem if the text changes. But I am interested in cases which works only for read only files. First of all I need that for log file analysis.
Vereb
+1  A: 

There is a plugin/script called mark:

Mark : a little script to highlight several words in different colors simultaneously. For example, when you are browsing a big program file, you could highlight some key variables. This will make it easier to trace the source code.

http://www.vim.org/scripts/script.php?script%5Fid=1238

danimajo
I believe the plugin is just an automatic way to create match groups for text you select/specify. Perhaps this is indeed what the OP wants, but note that it'll end up highlighting all instances of the matched text, not just the particular one you selected.
Jefromi
+1  A: 

The basic stuff to start from is:

:hi Green guibg=#33ff33
:syntax region Green start=/\%20l/ end=/\%30l/

What it does:

  1. Define 'Green' highlight group with green background color.
  2. Define syntax region which should be highlighted with 'Green' highlight group started from line nr 20 to line nr 30.

Now you can write a function or/and command which takes visually selected text and applies one of the multiple predefined color groups to it. Once you have that function -- bind it to your keys: for example \g for green, \r for red,

Upd:

And here is a bit of vimscript:

function! HighlightRegion(color)
  hi Green guibg=#77ff77
  hi Red guibg=#ff7777
  let l_start = line("'<")
  let l_end = line("'>") + 1
  execute 'syntax region '.a:color.' start=/\%'.l_start.'l/ end=/\%'.l_end.'l/'
endfunction

vnoremap <leader>g :<C-U>call HighlightRegion('Green')<CR>
vnoremap <leader>r :<C-U>call HighlightRegion('Red')<CR>

Note:

It can't reapply the highlighting (Green to Red for instance).

Maxim Kim
A: 

Looks like the Mark plugin does what you want. Once you get it installed, simply make a visual selection and press \m.

jamessan
+1  A: 

I believe the Txtfmt plugin is what you're looking for...

Txtfmt (The Vim Highlighter)

Txtfmt provides a sort of "rich text" capability for plain text in Vim. The highlighting is accomplished via hidden marker characters inserted directly into the buffer, so the highlighting is made persistent without the need to store metadata apart from the file.

Txtfmt is highly configurable. The default settings support 8 (configurable) foreground colors, 8 (configurable) background colors, and all combinations of bold, underline and italic attributes (e.g., bold, bold-italic, bold-underline, etc...). A non-default configuration supports the following additional attributes: standout, reverse and undercurl.

There is a very extensive help file, and the author is more than happy to answer usage questions...

Brett Stahlman