+60
Voltooid

upon selecting a word, highlight all occurrences

Enmanuel Rivera 14 jaar geleden bijgewerkt door Azhar eds 7 jaar geleden 41
upon selecting a word, highlight all occurrences of the same word in the current file. Many editors / IDEs already do this. It's a feature I have grown accoustomed, and I find it very difficult to work without.

Antwoord

Antwoord
Voltooid
Added in 2181
+2
So without pressing another key combo?
+1
Here's a plugin:


import sublime
import sublime_plugin

class WordHighlightListener(sublime_plugin.EventListener):
  def on_selection_modified(self,view):
    regions = []
    for sel in view.sel():
      if len(sel):
        regions += view.find_all(view.substr(sel))
    view.add_regions("WordHighlight", regions, "comment", 0)
Whoops, that broke the formatting. Let me upload it real fast.
Nice work!
Perhaps only highlight after 3 or more chars have been selected? And also make this customizable. I fear that selecting a single letter in a large file (>80MB) might ddos sublime :). And selecting a single letter is always happening when starting a selection via the cursor/arrow keys...
Also whitespaces should be stripped - at least I don't really see sense in selecting a massive amount of indents :).
I've changed it so it strip()s and checks the selection length, but it turns out that re.escape is the most worthless function in the world... I need to find another way to escape the strings before I push it to github. Just a moment...
Pushed. The number of characters a selection needs to be (>2) isn't a user pref, because I was lazy or whatever, but it's there. I also use sublime.LITERAL instead of re.escape—I'm a bit embarrassed to admit that I didn't know that flag existed.
Uhm... and strip doesn't work with LITERAL too?
I'm not sure I understand the question? Maybe it's because I didn't add the strip change before I committed and pushed...
Regardless, strip is now actually actually in there, and I also changed it so it only searches when you've got an entire word selected rather than when you've got a certain number of characters selected.
Hmmm... view.word(sel) feels much better. Also you don't need the strip anymore, it seems.
As it works now it feels right to me. Thanks!

Update: Found a small bug :). When having a word selected and clicking into an empty line, the regions won't be removed. I've added 'view.erase_regions("WordHighlight")' before 'view.add_regions(…)' to fix this.
Update 2: Added ' and not sel.empty()' at the end of the if clause to prevent regex errors on empty selections.
I think strip is still needed, because if you have a line that's only whitespace, then selecting that whole line (without the newline) will pass the len(sel) == len(view.word(sel)) check.
What about this?


    regions = []
    for sel in view.sel():
      #If we directly compare sel and view.word(sel), then in compares their
      #a and b values rather than their begin() and end() values. This means
      #that a leftward selection (with a > b) will never match the view.word()
      #of itself.
      #As a workaround, we compare the lengths instead.
      if len(sel) == len(view.word(sel)):
        sel = view.substr(sel).strip()
        if len(sel):
          regions += view.find_all(sel, sublime.LITERAL)
    view.add_regions("WordHighlight", regions, color_scope_name, draw_outlined)
This is to avoid 0-length searches? I just pushed a change that does that... :P
Can you add an option to your plugin that highlights the word without having to select characters, ie the word that the cursor is currently on (delimited by the standard word delimiters defined in the preferences)?
Done! There's a new option for it.
Thank you for this! It is wonderful!
One gripe, though: If I select a variable named foo, then it will also highlight the first half of variables named foobar. Is there any way to limit highlighting to full, complete names and not parts?
Your wish is my command.
Wow. Thank you greatly!

Also, looking at the code:
What a slick solution! I wouldn't have thought of that! (Mind you, I'm no good with regexes, but still!)

Is it still available? The github link to word_highlight.py doesn't seem to work.
Could someone create a version for ST 2, pretty please, otherwise, I'm loving this editor!
The only version of my plugin that exists is for ST2:

https://github.com/SublimeText/WordHighlight

+1
Could anyone, please, tell me how to install it. I tried and looked in relate sites for hours......

Download the zip from https://github.com/SublimeText/WordHighlight/zipball/master and unzip it into your packages directory.

I really like this plugin. Only problem that comes to mind, is that the highlights (I use boxed highlights) only appears/disappers in the sidebar when i scroll in the document/window.
I use OS X Lion.
Great work adzenith! This should totally be incorporated into Sublime.
This does happen now, since 2181 via the match_selection option.
Yes the latest build has this built in. I still prefer adzenith's plugin though, because it allows you to highlight other occurrences of the word that has the caret in it, without having to select it first, and I find that very handy.
Antwoord
Voltooid
Added in 2181
+2
Wouldn't it be nice if it supported several simultaneous highlights of different colors with some sort of navigation between them? For example, there's as highlight-mode plugin for emacs, where you can press a combo (say, Ctrl-F3) which highlights the word under the cursor with green, then you can move the cursor onto another word (the previous one is still highlighted with green), press Ctrl-F3 again, and the second word is highlighted in pink (the previous one is still highlighted in green), and so on and so forth. Furthermore, if you then put the cursor on a highlighted word and press F3, it will navigate to the next occurrence of the word highlighted with that color, and Alt-F3 navigates to the previous occurrence. I find that immensely convenient for analyzing source code...

Yes! This is the one feature I miss from vim, http://vim.wikia.com/wiki/Highlight_multiple_words.

I have this, it was kinda simple to implement. The only bad thing is that  you need to define fake theme colors (ST doesn't allow you to just pass a color as an hex string or something..)

Is there a way to select the highlighted matches?

Both find_under_expand and find_all commands select substring matches, besides the highlighted matches.

Hi,

Would it be possible to make it so that the definition of a "word" is defined according to the word_separators in the user's keybindings settings file?  I'm using R, and variable names can contain "." in them, so that foo.bar is considered as one word in the R language.  It would be great if the auto-highlighting respected this as well.


Thanks!

Hi,
I have install this on both sublime text 2 & 3 ...it worked when you select the text

but not when your cursor is on it.



i have modified my Word Highlight.sublime-settings according to read me instructions but having probs..can you please help...




{
    "color_scope_name": "comment",
    "case_sensitive": false,
    "draw_outlined": true,

    "mark_occurrences_on_gutter" : false,
    // valid types: dot, circle, bookmark and cross
    "icon_type_on_gutter" : "dot",

    "highlight_delay": 2,
    "highlight_when_selection_is_empty": true,
    "highlight_word_under_cursor_when_selection_is_empty": true,
    "highlight_non_word_characters":false,
    "show_word_highlight_status_bar_message" : true,    
    
    "file_size_limit": 4194304,
    "when_file_size_limit_search_this_num_of_characters": 20000
    
}


i have text and lots of duplicated instances of different
words across. It would have been really nice if most of the duplicated 
words are highlighted..

 

thanks