58 votes
 
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.
 
COMPLETED
updated 3 months ago
Added in 2181
 
+1
Oktay Acikalin
So without pressing another key combo?
Translation provided by Microsoft translator:

 
0
adzenith
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)
Translation provided by Microsoft translator:

 
0
adzenith
Whoops, that broke the formatting. Let me upload it real fast.
Translation provided by Microsoft translator:

 
+4
adzenith
Https://github.com/adzenith/Sublime-plugins/raw/master/word_highlight.py
Let me know if you've got any comments or suggestions. :)
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
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 :).
Translation provided by Microsoft translator:

 
0
adzenith
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...
Translation provided by Microsoft translator:

 
0
adzenith
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.
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
Uhm... and strip doesn't work with LITERAL too?
Translation provided by Microsoft translator:

 
0
adzenith
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.
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
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.
Translation provided by Microsoft translator:

 
0
adzenith
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.
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
Hmm... you're right.
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
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)
Translation provided by Microsoft translator:

 
0
adzenith
This is to avoid 0-length searches? I just pushed a change that does that... :P
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
Nice ^^
Translation provided by Microsoft translator:

 
0
Pravin Paratey
Thanks!
Translation provided by Microsoft translator:

 
0
Gavin Rehkemper
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)?
Translation provided by Microsoft translator:

 
0
adzenith
Done! There's a new option for it.
Translation provided by Microsoft translator:

 
0
Gavin Rehkemper
Perfect. Thank you.
Translation provided by Microsoft translator:

 
0
Centigonal The Circular
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?

Translation provided by Microsoft translator:

 
0
adzenith
Your wish is my command.
Translation provided by Microsoft translator:

 
0
Centigonal The Circular
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!)


Translation provided by Microsoft translator:

 
0
qay xsw
Thank you.
Translation provided by Microsoft translator:

 
0
vtrs
Is it still available? The github link to word_highlight.py doesn't seem to work.
Translation provided by Microsoft translator:

 
0
Gavin Rehkemper
One version of the archived code is available at my site: http://www.gavinr.com/2011/03/28/sublime-text-2-highlight-all-instances-of-words/
Translation provided by Microsoft translator:

 
+1
adzenith
Sorry, I broke it out into its own repo:

https://github.com/SublimeText/WordHighlight

Translation provided by Microsoft translator:

 
0
Gëzim H
Could someone create a version for ST 2, pretty please, otherwise, I'm loving this editor!
Translation provided by Microsoft translator:

 
0
adzenith
The only version of my plugin that exists is for ST2:

https://github.com/SublimeText/WordHighlight

Translation provided by Microsoft translator:

 
+1
Flake
Could anyone, please, tell me how to install it. I tried and looked in relate sites for hours......
Translation provided by Microsoft translator:

 
0
adzenith

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

Translation provided by Microsoft translator:

 
0
Allan H.
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.
Translation provided by Microsoft translator:

 
0
chaiguy
Great work adzenith! This should totally be incorporated into Sublime.
Translation provided by Microsoft translator:

 
0
James Brooks

This does happen now, since 2181 via the match_selection option.

Translation provided by Microsoft translator:

 
0
chaiguy
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.
Translation provided by Microsoft translator:

 
0
Nui Gurumi
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...
Translation provided by Microsoft translator:

Community stats

  • 11,129People
  • 2,414Feedback
  • 3,128Comments
  • 31,913Votes