upon selecting a word, highlight all occurrences
Answer
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)
Let me know if you've got any comments or suggestions. :)
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 :).
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.
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.
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)
Download the zip from https://github.com/SublimeText/WordHighlight/zipball/master and unzip it into your packages directory.
I use OS X Lion.
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
Customer support service by UserEcho