Enable option / disable option

15 views
Skip to first unread message

Dotan Cohen

unread,
Jul 31, 2012, 7:51:52 AM7/31/12
to vim use
On Superuser I found this nice way to highlight the word under the
cursor for the whole page:
:autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/',
escape(expand('<cword>'), '/\'))

http://superuser.com/questions/255024/vim-highlighting-a-search-term-without-moving-the-cursor

I would like to enable / disable this feature, for instance on
<leader>h. Is there any way to do this? I could write a function to
set the option, but how to disable it afterwards, and how to check the
state so that the same key could toggle?

Thanks.

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com

geoffr...@thomsonreuters.com

unread,
Jul 31, 2012, 8:23:35 AM7/31/12
to vim...@googlegroups.com
On Tuesday, 31 July 2012 12:51:52 UTC+1, dotancohen wrote:
> On Superuser I found this nice way to highlight the word under the

> cursor for the whole page:

> http://superuser.com/questions/255024/vim-highlighting-a-search-term-without-moving-the-cursor


>
> I would like to enable / disable this feature, for instance on
> <leader>h.

Here's another way to highlight the word under the cursor

http://vim.wikia.com/wiki/VimTip1572

If you use this plugin, you can simply enable the functionality

\m

then use a NumPad key to assign a colour to that
word everywhere without moving your cursor.

1

You can us many different highlights at once for many
different words, or more complex search patterns if you
wish.

Doesn't answer your programming question of course,
just evades it :)

regards,
Geoff

Benjamin R. Haskell

unread,
Jul 31, 2012, 8:24:07 AM7/31/12
to vim use
On Tue, 31 Jul 2012, Dotan Cohen wrote:

> On Superuser I found this nice way to highlight the word under the
> cursor for the whole page:
> :autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))
>
> http://superuser.com/questions/255024/vim-highlighting-a-search-term-without-moving-the-cursor
>
> I would like to enable / disable this feature, for instance on
> <leader>h. Is there any way to do this? I could write a function to
> set the option, but how to disable it afterwards, and how to check the
> state so that the same key could toggle?

" <Leader>h toggles the under-cursor highlighting
:map <Leader>h let g:under_cursor_hl = 1 - get(g:, 'under_cursor_hl', 1)

" Use it in the CursorMoved autocmd
:autocmd CursorMoved * if get(g:, 'under_cursor_hl', 1) | exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')) | end

Or am I missing something?

--
Best,
Ben

Dotan Cohen

unread,
Jul 31, 2012, 8:33:01 AM7/31/12
to vim...@googlegroups.com
That's fine, I'm married and thus used to my questions being evaded :)

The problem with that plugin is:
א) It requires leaving the home row, and
ב) I don't have a numpad on some keyboards that I use!

Dotan Cohen

unread,
Jul 31, 2012, 8:41:27 AM7/31/12
to vim...@googlegroups.com
On Tue, Jul 31, 2012 at 3:24 PM, Benjamin R. Haskell <v...@benizi.com> wrote:
> " <Leader>h toggles the under-cursor highlighting
> :map <Leader>h let g:under_cursor_hl = 1 - get(g:, 'under_cursor_hl', 1)
>
> " Use it in the CursorMoved autocmd
> :autocmd CursorMoved * if get(g:, 'under_cursor_hl', 1) | exe printf('match
> IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')) | end
>
> Or am I missing something?
>

Thanks, Ben. The problem is that I cannot turn the feature off! That
was the question in the OP, with a different method of achieving the
same effect.

My ultimate goal is to be able to set specific words to be
highlighted, say toggling them with \hw (highlight word), and _also_
to have the ablility to toggle highlight-word-under-cursor, say with
\hh.

Benjamin R. Haskell

unread,
Jul 31, 2012, 8:53:04 AM7/31/12
to vim...@googlegroups.com
On Tue, 31 Jul 2012, Dotan Cohen wrote:

> On Tue, Jul 31, 2012 at 3:24 PM, Benjamin R. Haskell <v...@benizi.com> wrote:
>> " <Leader>h toggles the under-cursor highlighting
>> :map <Leader>h let g:under_cursor_hl = 1 - get(g:, 'under_cursor_hl', 1)
>>
>> " Use it in the CursorMoved autocmd
>> :autocmd CursorMoved * if get(g:, 'under_cursor_hl', 1) | exe printf('match
>> IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')) | end
>>
>> Or am I missing something?
>>
>
> Thanks, Ben. The problem is that I cannot turn the feature off! That
> was the question in the OP, with a different method of achieving the
> same effect.

Ah. I didn't understand that you wanted the feature to be completely
eradicated, rather than just turned off (which toggling accomplishes).


> My ultimate goal is to be able to set specific words to be
> highlighted, say toggling them with \hw (highlight word), and _also_
> to have the ablility to toggle highlight-word-under-cursor, say with
> \hh.

Not sure quite what you want w/ the \hw portion, but for the \hh, put
the autocmd in an augroup. Probably not the most elegant VimL, but
it'll work:

fun! EnableUnderCursorHighlighting()
let g:under_cursor_hl_enabled = 1
aug UnderCursorHighlighting
au!
au CursorMoved * if get(g:, 'under_cursor_hl', 1) | exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')) | end
aug END

let g:under_cursor_hl = 1

map <Leader>h let g:under_cursor_hl = 1 - g:under_cursor_hl
endf

fun! DisableUnderCursorHighlighting()
unlet g:under_cursor_hl_enabled
aug UnderCursorHighlighting
au!
aug END
unmap <Leader>h
endf

fun! ToggleUnderCursorHighlighting()
if exists('g:under_cursor_hl_enabled')
call EnableUnderCursorHighlighting()
else
call DisableUnderCursorHighlighting()
end
endf

map <Leader>hh call ToggleUnderCursorHighlighting()

--
Best,
Ben
Reply all
Reply to author
Forward
0 new messages