azabiong / vim-highlighter

Highlight words and expressions
MIT License
240 stars 11 forks source link

Add highlight programmaticaly #5

Closed mpasa closed 2 years ago

mpasa commented 2 years ago

First of all, thank you for the work on the plugin, it's very helpful. I have been checking the documentation, and I think what I am trying to do is not supported, but let me know if it is.

I would like to highlight some text programmatically when opening files. For example, using a command like :Hi + foo will highlight all the foo words in the document, without the cursor being on top of the the word at that moment, which seems to be the only way to achieve this currently.

azabiong commented 2 years ago

Hi, Thank you for using this tool, and I'm glad you found it's helpful.

The ability to set highlights using expressions is still in my upgrade list. I'm trying to find a good idea on how to easily erase the highlights when set using regex.

For now, it is possible to apply the pattern indirectly using Hi:save and Hi:load command. I think you can try the following steps:  

Firstly open a file, and clear highlights if set. Select a word, and set highlighting, and then save it using Hi:save command with any filename, for example:

    :Hi:save test

Now open the file test.hl using:

    :Hi ls

When you open the file, you will see that the highlight pattern is saved in the following format:

    color_number : \V word

Now, you can use this form to save your patterns, For example:

    :!echo '7:\VHello' > ./hello.hl

And then, you can load it using the command:

    :Hi:load ./hello
azabiong commented 2 years ago

I have another idea.

First, let's add the feature you suggested.

: Hi+ pattern

And for the erase function, currently the HiErase key can erase highlight patterns containing simple expressions. For example:

\vHello \w+ for Hello World

However, some complex patterns may or may not be erased. But still the HiClear key will erase all the highlights. I think the ability to set the expression when needed is more useful than the perfectly matched erase function.

So, how about adding this feature and setting it to "may or may not be erased"?

mpasa commented 2 years ago

For my specific use case, having just a simple :Hi+ pattern will make me happy. I usually just clear all the patterns at the same time, or manually with the cursor under the highlight. Of course, I will let you decide whether having something it's considered incomplete is the best for the project or not.

azabiong commented 2 years ago

Thanks for your comment. And then, I will add the feature and let you know right away.

azabiong commented 2 years ago

Now it's ready. Please see the brief summary:

  :h Hi+

Hopefully you like the feature.

mpasa commented 2 years ago

Oh, that was fast! Thank you a lot for your work. It works like a charm for my use case. You can feel free to close the issue.

azabiong commented 2 years ago

Nice, I hope this tool helps, and be happy. Thank you

azabiong commented 2 years ago

Hi Miguel, I would like to inform you that a very useful feature has been added to the plugin. Now it supports jumping to the recently set highlights using shortcuts. Please find the brief help :h Hi> and I would like to recommend that you try the suggested key map at first time. I find it really convenient.