Sublime Forum

Match_selection of a few words?

#1

Hi friends,
is it possible to highlight selection for several words? E.g., if I select one word, ST automatically highlight all this words among the text. But sometime I need to compare two long strings (like hashes etc) with any kind of delimiters (spaces, slashes etc) and ST highlight doesn’t work in this case.
Npp, VScode works fine with this and I really miss this behavior in ST.

Did I miss any respective settings? The only thing which I found is match_selection.

Thnx

1 Like

#2

Selection highlighting is limited to words only in ST. The intention is to reduce the number of false positives.

A related issue can be found at:

0 Likes

#3

Thnx for the explanation, deathaxe
This limitation makes such functionality almost useless. The most case which I’m interested in consists of words sequences or letters sequences which include special symbols which ST consider as words separators.
Also, as I already mentioned, Npp, VSCode works in this way without any problems.

0 Likes

#4

We can just upvote the issue and try to describe sane use cases and ways how to handle that, I guess.

1 Like

#5

Something like this plugin might help; it provides a command that takes any selected text and highlights all of the instances of it. Running the command with a single, empty selection clears all of the marks.

This video shows how to set up a custom plugin like this, if you’re unfamiliar with that.

The color used for the highlight is configurable as well.

import sublime
import sublime_plugin


class ShowMatchesCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # If there is a single empty selection, remove all marks
        if len(self.view.sel()) == 1 and not self.view.sel()[0]:
            return self.view.erase_regions('marked_sel')

        matches = []
        for sel in self.view.sel():
            text = self.view.substr(sel)
            found = self.view.find_all(text, sublime.LITERAL)
            matches.extend(found)

        self.view.add_regions('marked_sel', matches, 'region.redish')

Example key binding:

    { "keys": ["ctrl+alt+t"], "command": "show_matches" },
3 Likes

#6

Thnx @OdatNurd
I’ll try your plugin

Actually the main gist of my message was to find native solution. As workaround I can use Search action. But it requires some additional clicks.

0 Likes

#7

Has anything changed since the time this topic was created? Maybe there’s at least a way to make the above plugin work at all times and not just when called by a key bind?

0 Likes

#8

Nothing has changed about this as far as I’m aware (or at least I presume so since you’re asking about it).

However, if you want this to happen all the time and not just when you press a key, you can add this to the bottom of the plugin:

import functools
class DoItAlways(sublime_plugin.ViewEventListener):
    pending = 0

    def on_selection_modified_async(self):
        # Sanity check; only do this for smaller-ish files; may need tweaking.
        if len(self.view) < 1048576:
            self.pending += 1
            sublime.set_timeout_async(functools.partial(self.update_find), 250)

    def update_find(self):
        self.pending -= 1
        if self.pending != 0:
            return

        self.view.run_command("show_matches")

This says that 250ms after the last time the selection in the file is modified, run the command to update matches, but only if the file is smaller than 1mb.

You can tune the size of the file as desired as well as the timeout value. The smaller you cange the 250 to, the faster that the selection change will respond at the expense of having to do it more frequently if you make a lot of selection changes.

Similarly, you can make the file size sanity check as large or as small as you would like; it’s there to make sure that Sublime doesn’t hang if you open a very large file and then select a single character which repeats very frequently.

I didn’t tune either of these values here, I just made them up. Adjust as according for your particular machine setup and use case.

2 Likes

#9

Thank you, it works.

0 Likes