Highlight more than one character?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Highlight more than one character?

Mike-111
How do I highlight more than one character at the same time?

Lets say I have a script that has collected a character location
represented by the variables line1, col1.  I can highlight that
character by doing something like:

    hi PosHL guifg=red guibg=yellow gui=bold
    exe 'match PosHL /\%'.line1.'l\%'.col1.'c/'

But what if I have also collected another character that I want
to higlight, saved in the variables line2, col2.  If I do:

    exe 'match PosHL /\%'.line2.'l\%'.col2.'c/'

then it will highlight the second character, but then the highlight for
the first character goes away.  How do I highlight them both at the same
time? I've tried variations of the following, but haven't figured
out anything that works yet:

    exe 'match PosHL /\%'.line1.'l\%'.col1.'c,\%'.line2.'l\%'.col2.'c/'

And the more general case would be:  How do I highlight any arbitrary
number of characters at the same time?

Thanks, Mike
Reply | Threaded
Open this post in threaded view
|

Re: Highlight more than one character?

Tim Chase-2
>     exe 'match PosHL /\%'.line1.'l\%'.col1.'c,\%'.line2.'l\%'.col2.'c/'

Ever so close!  Instead of a comma, you want "\|", making it

exe 'match PosHL /\%'.line1.'l\%'.col1.'c\|\%'.line2.'l\%'.col2.'c/'

(all on one line in case mailers bung it on either end of things)

        :help /bar

> And the more general case would be:  How do I highlight any
> arbitrary number of characters at the same time?

A function like the following

   let b:pos = ""
   function! AddLocation()
     if b:pos != ""
       let b:pos = b:pos.'\|'
     endif
     let b:pos = b:pos.'\%'.line(".").'l\%'.col(".").'c'
     exe 'match PosHL /'.b:pos.'/'
   endfunction

will track previously selected items in the b:pos variable and
then chain them together for the match statement.  Each time you
call it, it will mark your current location.  Adjust the line()
and col() accordingly if you'd rather pass in parameters for
places your cursor currently isn't.

-tim





Reply | Threaded
Open this post in threaded view
|

Re: Highlight more than one character?

A.J.Mechelynck
In reply to this post by Mike-111
Mike wrote:

> How do I highlight more than one character at the same time?
>
> Lets say I have a script that has collected a character location
> represented by the variables line1, col1.  I can highlight that
> character by doing something like:
>
>     hi PosHL guifg=red guibg=yellow gui=bold
>     exe 'match PosHL /\%'.line1.'l\%'.col1.'c/'
>
> But what if I have also collected another character that I want
> to higlight, saved in the variables line2, col2.  If I do:
>
>     exe 'match PosHL /\%'.line2.'l\%'.col2.'c/'
>
> then it will highlight the second character, but then the highlight for
> the first character goes away.  How do I highlight them both at the same
> time? I've tried variations of the following, but haven't figured
> out anything that works yet:
>
>     exe 'match PosHL /\%'.line1.'l\%'.col1.'c,\%'.line2.'l\%'.col2.'c/'
>
> And the more general case would be:  How do I highlight any arbitrary
> number of characters at the same time?
>
> Thanks, Mike
>
>
>

Only one ":match" at a time can be active. But its pattern may contain
several or'ed branches. Example: To highlight the words "foot" or "feet"
wherever they appear:

        :hi Walking ctermbg=cyan ctermfg=white guibg=#33CCFF guifg=white
        :match Walking /\<foot\>\|\<feet\>/

see ":help pattern"


HTH,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: Highlight more than one character?

Tim Chase-2
In reply to this post by Tim Chase-2
Sorry for the reply to my own post...

> function! AddLocation()

You can also map this

        :map <f4> :call AddLocation()<cr>

which makes something that allows you to hit <f4> and flag the
character under the cursor.  For some reason, this strikes me
disturbingly as a "bird poop" function, like watching my car
under a tree with all these little highlighted droppings
appearing on my document :)

To clear its memory, you can reset the b:pos variable to "".

-tim





Reply | Threaded
Open this post in threaded view
|

Re: Highlight more than one character?

Mike-111
In reply to this post by Tim Chase-2
> Ever so close!  Instead of a comma, you want "\|", making it
>
> exe 'match PosHL /\%'.line1.'l\%'.col1.'c\|\%'.line2.'l\%'.col2.'c/'
>
> (all on one line in case mailers bung it on either end of things)
>
>         :help /bar
>
> > And the more general case would be:  How do I highlight any
> > arbitrary number of characters at the same time?
>
> A function like the following
>
>    let b:pos = ""
>    function! AddLocation()
>      if b:pos != ""
>        let b:pos = b:pos.'\|'
>      endif
>      let b:pos = b:pos.'\%'.line(".").'l\%'.col(".").'c'
>      exe 'match PosHL /'.b:pos.'/'
>    endfunction
>
> will track previously selected items in the b:pos variable and
> then chain them together for the match statement.  Each time you
> call it, it will mark your current location.  Adjust the line()
> and col() accordingly if you'd rather pass in parameters for
> places your cursor currently isn't.

That works great, thanks Tim

I'm going to bake this into the stuff I'm working on from the other thread
that I started about brace matching.

-Mike
Reply | Threaded
Open this post in threaded view
|

Re: Highlight more than one character?

Tim Chase-2
>>exe 'match PosHL /\%'.line1.'l\%'.col1.'c\|\%'.line2.'l\%'.col2.'c/'
>
> That works great, thanks Tim
>
> I'm going to bake this into the stuff I'm working on from the
> other thread that I started about brace matching.

Glad it helps.

One caveat regarding your brace-matching adventures: if the line
with highlighting in it gets shifted left or right, the
highlighting stays in the same place (unless updated), off by the
ammt of indent/exdent.

-tim