Search code examples
sublimetext3sublimetextsublimetext4

Select or Highlight all instances of a word type or color in Sublime Text 4


Note: I am not trying to select all instances of a single word.

I can not find a way to select all instances of a word type, similarly how different words are colored according to the format rules from the file type you have.

Here is an image of some sample code of a SQL file.

Sample code of SQL file in Sublime Text 4

For example, I would like to Highlight all instances of this reddish pink color, or the baby blue colored words in the whole file. So that way I can capitalize them or copy them or what have you.


Solution

  • From the Tools menu -> Developer -> New Plugin...

    Replace the template with the following:

    import sublime
    import sublime_plugin
    
    
    class SelectByScopeSelectorCommand(sublime_plugin.TextCommand):
        def run(self, edit, scope_selector=None, last_scope_only=True, auto_select=False):
            if not scope_selector:
                scope_at_first_caret = self.view.scope_name(self.view.sel()[0].a)
                if last_scope_only:
                    scope_at_first_caret = scope_at_first_caret.split()[-1]
    
                if auto_select:
                    scope_selector = scope_at_first_caret
                else:
                    self.view.window().show_input_panel('Scope Selector', scope_at_first_caret, lambda value: self.view.run_command('select_by_scope_selector', { 'scope_selector': value }), None, None)
                    return
    
            regions = self.view.find_by_selector(scope_selector)
            if regions:
                self.view.sel().clear()
                self.view.sel().add_all(regions)
                self.view.show_at_center(self.view.sel()[0])
            else:
                self.view.window().status_message('Unable to find anything matching selector "' + scope_selector + '"')
    

    Save it, in the folder ST recommends, as something like select_by_selector.py (the filename doesn't matter too much, but the extension is important).

    Then, in your User keybindings, you can add something like:

    { "keys": ["alt+;"], "command": "select_by_scope_selector", "args": { "last_scope_only": true, "auto_select": true } },
    

    Then, pressing Alt+; with the selection caret somewhere in SELECT, it will automatically select all other words in the buffer with the same scope, like DELETE, UPDATE, INSERT etc. Or on INT, it could select all INT, CHAR etc.

    You may notice that the "types" you referred to in your question are called scopes in ST parlance. Note that colors don't necessarily have a one to one mapping with scopes depending on your color scheme, so it may select more or less than you expect it to.

    You can play around with the keybinding, by removing all arguments for example, to see what effect it has and customize which scopes are being searched for. You can also add it to a menu or the command palette, if that is more to your liking. I suggest to read the ST docs for more info.