how can I configure word highlighting in VIM ?

48 views
Skip to first unread message

Arup Rakshit

unread,
Mar 29, 2014, 5:09:35 PM3/29/14
to vim...@googlegroups.com
Hi,

I use vim to write my Ruby code. Say, I wrote a code as below :

foo = 2
def meth(arg)
puts arg
end

meth(foo)

Now suppose, I defined **foo** in the line#4. Now I am calling the method with **foo** as an argument, from line #20. Now suppose I am selecting the word, **foo** using command **viw**. Now I want all `foo` to be highlighted. If this can be done, it would help me in lots of scenarios.

John Beckett

unread,
Mar 29, 2014, 10:42:05 PM3/29/14
to vim...@googlegroups.com
Arup Rakshit wrote:
> Subject: how can I configure word highlighting in VIM ?

If you mean by searching, I suggest starting here:

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

See what it says (you know about "*"?), and scan down to the
"Highlighting search matches" link near the bottom. There are
also some search-without-moving ideas there somewhere.

To just highlight, see:

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

John


Arup Rakshit

unread,
Mar 30, 2014, 2:26:50 AM3/30/14
to vim...@googlegroups.com
I found this feature, long back in Notepad++. In that editor, if you select any word, it will highlight all occurrence of that word. It sometime helps to verify if at the line# 100 any variable I am using, it will confirm, if that was defined previously or not.




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/hXcGlddeUbM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
*---------------------------------------------------------------*
*Warm Regards,*

*Arup Rakshit*
*Software Engineer*



tooth pik

unread,
Mar 30, 2014, 2:51:15 AM3/30/14
to vim...@googlegroups.com
On Sun, Mar 30, 2014 at 11:56:50AM +0530, Arup Rakshit wrote:
> I found this feature, long back in Notepad++. In that editor, if you select
> any word, it will highlight all occurrence of that word. It sometime helps
> to verify if at the line# 100 any variable I am using, it will confirm, if
> that was defined previously or not.

you did not respond to John's question: you know about "*"?, and your
re-query implies you don't

if you navigate to the word you want highlighted, press * , that word
will be searched for, and by virtue of its now being in the @/ register,
all occurrences of that word will now by default be highlighted, and you
can re-search with n and N

--
_|_ _ __|_|_ ._ o|
|_(_)(_)|_| ||_)||<
|

Jürgen Krämer

unread,
Apr 1, 2014, 3:52:07 AM4/1/14
to vim...@googlegroups.com

[Text re-ordered; please don't top-post]

Hi,

Arup Rakshit wrote:
>
> On Sun, Mar 30, 2014 at 8:12 AM, John Beckett <johnb....@gmail.com <mailto:johnb....@gmail.com>> wrote:
>>
>> Arup Rakshit wrote:
>> > Subject: how can I configure word highlighting in VIM ?
>>
>> If you mean by searching, I suggest starting here:
>>
>> http://vim.wikia.com/wiki/Searching
>>
>> See what it says (you know about "*"?), and scan down to the
>> "Highlighting search matches" link near the bottom. There are
>> also some search-without-moving ideas there somewhere.
>>
>> To just highlight, see:
>>
>> http://vim.wikia.com/wiki/Highlight_multiple_words
>
> I found this feature, long back in Notepad++. In that editor, if you
> select any word, it will highlight all occurrence of that word. It
> sometime helps to verify if at the line# 100 any variable I am using,
> it will confirm, if that was defined previously or not.

you can put the following lines in a script file in your plugins directory:

highlight default link CurrentWord IncSearch

function! s:HighlightCurrentWord(on)
if a:on
augroup HighlightCurrentWord
au CursorMoved,CursorMovedI * exe 'match CurrentWord /\<' . escape(expand('<cword>'), '\/.*$^~[]') . '\>/'
augroup END
else
augroup HighlightCurrentWord
au!
match NONE
augroup END
endif
endfunction

command! -bang HiCurWord call s:HighlightCurrentWord(expand('<bang>') != '!')

Then, if you execute ":HiCurWord", all occurences of the word under the
cursor will get highlighted. If you move the cursor to a different word,
the new word will get highlighted.

":HiCurWord!" switches highlighting off.

If you want to save some key strokes, define mappings like

nnoremap <F12> :HiCurWord<cr>
nnoremap <S-F12> :HiCurWord!<cr>

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)
Reply all
Reply to author
Forward
0 new messages