persistent highlight between marks ?

26 views
Skip to first unread message

M Kelly

unread,
Jun 13, 2020, 8:21:54 AM6/13/20
to vim_use
Hi,

Anyone know of a way to set a start mark and then an end mark and then highlight all text between them ?
So that is stays highlighted, even if I move the cursor away or scroll etc. 
Sort of like a visual mode selection that remains after you move away.

thx for all things vim,
-m

Jürgen Krämer

unread,
Jun 14, 2020, 10:01:51 AM6/14/20
to vim mailing list

Hi,

M Kelly schrieb am 13.06.2020 um 14:21:

Anyone know of a way to set a start mark and then an end mark and then highlight all text between them ?
So that is stays highlighted, even if I move the cursor away or scroll etc. 
Sort of like a visual mode selection that remains after you move away.

you can use the "/%'m" atom in a search or in the pattern of a :match command, e.g.,

  /%'a\_.*%'b/

or

  :match ErrorMsg /%'a\_.*%'b/

This will highlight everything from mark 'a up to, but not including mark 'b. If you want to include the position where you have put mark 'b, add a dot at the end of the pattern, i.e.,

  /%'a\_.*%'b./

If you change one of the marks after that, you might need to press Ctrl-L to refresh the display.

Regards,
Jürgen

Charles Campbell

unread,
Jun 29, 2020, 2:18:00 PM6/29/20
to vim...@googlegroups.com
See :help matchadd for a general solution.

Regards,
Chip Campbell

Tim Chase

unread,
Jun 29, 2020, 3:09:51 PM6/29/20
to Charles Campbell, vim...@googlegroups.com
On 2020-06-29 14:17, Charles Campbell wrote:
> M Kelly wrote:
>> Anyone know of a way to set a start mark and then an end mark and
>> then highlight all text between them ?
>> So that is stays highlighted, even if I move the cursor away or
>> scroll etc.
>> Sort of like a visual mode selection that remains after you move
>> away.
>
> See :help matchadd for a general solution.

You can do something like

:match Error /\%'<\|\%'>\|\%>'<\%<'>/

to keep the visually-selected range highlighted. If you visually
select something else, use control+L or :redraw to repaint the range.

If you have marks x and y, you can do the same thing (though this
requires that mark x preceeds mark y in the file, otherwise you just
get the endpoints highlighted):

:match Error /\%'x\|\%'y\|\%>'x\%<'y/

Adust "Error" to your favorite highlight group.

That breaks down as matching one of three things (separated by "\|"):

\%'x " the first mark
\%'y " the second mark
\%>'x\%<'y " stuff after 'x but before 'y

-tim




Reply all
Reply to author
Forward
0 new messages