![](/s/cache/cc/f7/ccf7f5396da885e4383165ea47f8679b.png)
upon selecting a word, highlight all occurrences
Svar
![](https://cdn.userecho.com/characters/74.png?1)
![](https://cdn.userecho.com/characters/97.png?1)
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)
![](https://cdn.userecho.com/characters/97.png?1)
Let me know if you've got any comments or suggestions. :)
![](/s/cache/a9/9b/a99b2d9458eebaa6387ad4bf399c5b6c.png)
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 :).
![](https://cdn.userecho.com/characters/97.png?1)
![](https://cdn.userecho.com/characters/97.png?1)
![](https://cdn.userecho.com/characters/97.png?1)
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.
![](/s/cache/a9/9b/a99b2d9458eebaa6387ad4bf399c5b6c.png)
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.
![](https://cdn.userecho.com/characters/97.png?1)
![](/s/cache/a9/9b/a99b2d9458eebaa6387ad4bf399c5b6c.png)
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)
![](https://cdn.userecho.com/characters/97.png?1)
![](https://cdn.userecho.com/characters/71.png?1)
![](https://cdn.userecho.com/characters/67.png?1)
![](https://cdn.userecho.com/characters/67.png?1)
![](https://cdn.userecho.com/characters/71.png?1)
![](https://cdn.userecho.com/characters/97.png?1)
![](/s/cache/50/49/5049c5055d31f047b634ac8002778256.png)
![](https://cdn.userecho.com/characters/97.png?1)
![](https://cdn.userecho.com/characters/70.png?1)
![](https://cdn.userecho.com/characters/97.png?1)
Download the zip from https://github.com/SublimeText/WordHighlight/zipball/master and unzip it into your packages directory.
![](/s/cache/1c/a9/1ca9e535b6cc8c6f09721b575ee534d0.png)
I use OS X Lion.
![](/s/cache/0d/d0/0dd00fd087056b92008a49a2a80ebd66.png)
![](https://cdn.userecho.com/characters/78.png?1)
![](https://cdn.userecho.com/characters/66.png?1)
Yes! This is the one feature I miss from vim, http://vim.wikia.com/wiki/Highlight_multiple_words.
![](/s/cache/38/30/3830e86f2bd6362c4288772c7b00fa6f.png)
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..)
![](https://cdn.userecho.com/characters/97.png?1)
Is there a way to select the highlighted matches?
Both find_under_expand and find_all commands select substring matches, besides the highlighted matches.
![](/s/cache/ff/bf/ffbff7958f9988641929ecb7b1c74664.png)
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!
![](https://cdn.userecho.com/characters/65.png?1)
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
Kundesupport af UserEcho