DESCRIPTION
This plugin changes the star command *, so that it doesn't jump to the next
match. (Unless you supply a [count], so 1* now restores the old * behavior.)
If you issue a star command on the same text as before, the search
highlighting is turned off (via :nohlsearch); the search pattern remains
set, so a n / N command will turn highlighting on again. With this, you
can easily toggle highlighting for the current word / visual selection.

With the disabling of the jump to the next match, there is no difference
between * and # any more, so the # key can now be used for some other mapping.

This plugin also extends the star command to visual mode, where instead of
the current word, the selected text is used as the literal search pattern.

The auto-search functionality instantly highlights the word under the cursor
when typing or moving around. This can be helpful while browsing source code;
whenever you position the cursor on an identifier, all other occurrences are
instantly highlighted. This functionality is toggled on/off via <Leader>*. You
can also :nohlsearch to temporarily disable the highlighting.

SEE ALSO
- mark.vim (vimscript #2666) can highlight several patterns in different
colors simultaneously.
- SearchAlternatives.vim (vimscript #4146) can add and subtract search
alternatives via mappings and commands.
- To also show the number of matches when selecting a word (*, g* etc.),
you can append the corresponding command to the <Plug> mapping (see
SearchHighlighting-remap):
nmap <silent> * <Plug>SearchHighlightingStar:%s///gn<CR>
The SearchPosition.vim plugin (vimscript #2634) provides an extended
version of that command.

RELATED WORKS
I came up with this on my own; however, the idea can be traced back to
francoissteinmetz and da.thompson in the comments of vimtip #1 (now at
http://vim.wikia.com/wiki/VimTip1):
map <silent> <F10> :set invhls<CR>:let @/="<C-r><C-w>"<CR>

USAGE
* Toggle search highlighting for the current whole
\<word\> on/off.
g* Toggle search highlighting for the current word
on/off.

{Visual}* Toggle search highlighting for the selection on/off.
For a blockwise-visual selection, not just lines
that match the block's lines on their own are matched,
but all occurrences that contain the block's text,
though not just with the same vertical alignment (that
isn't possible to assert with Vim's regular
expressions), and only the first line's match is
highlighted. For example, a match on
foo
bar
will also match all three occurrences in
foo my foo is tell foo
bar inside bar bar
but not a single
foo

[count]* Search forward for the [count]'th occurrence of the
[count]g* word nearest to the cursor.
{Visual}[count]*

[count],* Search forward for the [count]'th occurrence of the
word under the cursor, keeping the cursor at the
current position within the word's matches (via
search-offset).
Observes the new non-jumping behavior of the star
commands without a [count].

<Leader>* Toggle auto-search highlighting (using the last
{what}; default is "wword").
{Visual}<Leader>* Toggle auto-search highlighting; when turning on, a
{what} value of "selection" is used (but the preset
isn't changed; the original value is used for a later
non-visual mode auto-search).

:SearchAutoHighlighting [{what}]
Turn on automatic highlighting of occurrences of
{what} in normal mode, and the selected text in visual
/ select mode. Possible values:
wword: whole cword, like star; this is the default
wWORD: whole cWORD, delimited by whitespace
cword: current word under cursor
cWORD: current WORD under cursor
line: current text in line, excluding indent and
trailing whitespace
exactline: exact current line
selection: only highlight selected text, in normal
mode, nothing is selected

:NoSearchAutoHighlighting
Turn off automatic highlighting.

install details

INSTALLATION
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
vim SearchHighlighting*.vmb.gz
:so %
To uninstall, use the :RmVimball command.

- ENH: Add ,* search that keeps the current position within the current word when jumping to subsequent matches.
- Correctly emulate * behavior on whitespace-only lines where there's no cword: Issue "E348: No string under cursor".
- Add dependency to ingo-library (vimscript #4433). *** You need to separately install ingo-library (vimscript #4433) version 1.015 (or higher)! ***
- For a blockwise visual selection, don't just match the block's lines on their own, but also when contained in other text.
- BUG: For {Visual}*, a [count] isn't considered.