Highlight text ranges in Vim

Is it possible to mark a range of text in Vim and change the highlight color (to red), than select a different range of text and change this color (to green), preserving the previous highlight, etc.?

+4
source share
5 answers

I believe the Txtfmt plugin is what you are looking for ...

Txtfmt (Vim Designation)

Txtfmt provides a kind of "rich text" for plain text in Vim. The selection is carried out using hidden marker characters inserted directly into the buffer, so the selection is performed continuously, without the need to store metadata separately from the file.

Txtfmt is very customizable. The default settings support 8 (customizable) foreground colors, 8 (customizable) background colors, and all combinations of bold, underlined, and italic attributes (e.g. bold, bold, bold, bold, etc.). A configuration other than the default settings 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 questions about use ...

+5
source

The main material to start with:

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

What does he do:

  • Define the selection group "Green" with a green background.
  • Define the syntax area to be highlighted using the Green selection group, starting from line number 20 to line number 30.

Now you can write a function or / and a command that accepts visually selected text and applies one of several predefined color groups to it. When you have this function, attach it to your keys: for example, \ g for green, \ r for red,

Upd:

And here is a little 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 :<CU>call HighlightRegion('Green')<CR> vnoremap <leader>r :<CU>call HighlightRegion('Red')<CR> 

Note:

It is not possible to reapply backlighting (for example, from green to red).

+12
source

There is a / script plugin called mark:

Note: a little script to highlight multiple words in different colors at the same time. For example, when you are viewing a large program file, you can highlight some key variables. This will make tracking source code easier.

http://www.vim.org/scripts/script.php?script_id=1238

+5
source

Looks like the Flag plugin does what you want. Once you install it, just make a visual selection and press \ m .

+1
source

I am not aware of any way to do something like this, as this will require storing metadata that is not related to the actual contents of the file. Even if it only works in memory while Vim is running, I don’t know how to do it.

This does not mean that there is no way, since my knowledge of Vim is limited. But this is similar to what Wim did not.

0
source

All Articles