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?
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.
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.
The basic stuff to start from is:
:hi Green guibg=#33ff33
:syntax region Green start=/\%20l/ end=/\%30l/
What it does:
- Define 'Green' highlight group with green background color.
- 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).
Looks like the Mark plugin does what you want. Once you get it installed, simply make a visual selection and press \m.
I believe the Txtfmt plugin is what you're looking for...
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...