How to prevent asterisk key from advancing to next match when pressed

57 views
Skip to first unread message

Slartibartfast

unread,
Dec 14, 2016, 5:02:22 AM12/14/16
to vim_use
I frequently use the asterisk key (*) in conjunction with 'hlsearch' to highlight all occurrences of a word in a file. Normally when I do this I don't actually want to advance to the next match so I use 'nnoremap' to automatically wind back to the occurrence I was on when I pressed the key, as in:
:set nnoremap * *N

For the most part this works fine, however if the next occurrence of a match is off the screen the 'N' command does indeed bring you back to the correct place but that line is then placed at the top of the screen. One can of course remap to "*Nzz" to then bring that line to the center of the screen but that's a bit of a kludge as well.

Anyone know a way to achieve setting the search word like this without causing the screen to be scrolled? I'm sure it must be possible but I'm having trouble working it out myself.

Thanks.

Tim Chase

unread,
Dec 14, 2016, 7:18:02 AM12/14/16
to Slartibartfast, vim_use
On 2016-12-14 01:03, Slartibartfast wrote:
> Anyone know a way to achieve setting the search word like this
> without causing the screen to be scrolled? I'm sure it must be
> possible but I'm having trouble working it out myself.

Could do something like

:nnoremap * :let @/='\<lt>'.expand('<lt>cword>').'\>'<cr>

-tim



Slartibartfast

unread,
Dec 14, 2016, 7:34:52 AM12/14/16
to vim_use, his.royal...@gmail.com
> :nnoremap * :let @/='\<lt>'.expand('<lt>cword>').'\>'<cr>

Wow, what on earth does that mean?

I tried it all the same and yes it does indeed work in gVim but I have no idea why.

I also tried it in Notepad++ and Android Studio (with their respective Vim plugins) and unfortunately it doesn't for either of them.

Tim Chase

unread,
Dec 14, 2016, 8:41:04 AM12/14/16
to vim...@googlegroups.com
On 2016-12-14 04:34, Slartibartfast wrote:
> > :nnoremap * :let @/='\<lt>'.expand('<lt>cword>').'\>'<cr>
>
> Wow, what on earth does that mean?
>
> I tried it all the same and yes it does indeed work in gVim but I
> have no idea why.

It remaps the "*" key to run the ex command that follows.

let @/= " assign to the search register
'\<lt>' " a literal \<
. " followed by
expand(...) " the current word under the cursor
'<lt>cword>' " (need to escape the opening < sign)
. " followed by
'\>' " a literal \>
<cr> " actually execute the assignment

> I also tried it in Notepad++ and Android Studio (with their
> respective Vim plugins) and unfortunately it doesn't for either of
> them.

Can't speak to the vim-nature of non-vim. Sorry ;-)

-tim



kamaraju kusumanchi

unread,
Dec 16, 2016, 12:06:43 AM12/16/16
to vim...@googlegroups.com
The following mapping searches for a string on a double click.

nnoremap <silent> <2-LeftMouse> :let @/='\V'.escape(expand('<cword>'),
'\').''<cr>:set hls<cr>

It also does not add that word to the search history. For example if
you use the above mapping and double click on word "foo". It will
highlight all occurrences of foo in the file. But if you do /<UP>, it
will not list foo.

I find the above mapping very helpful when reading a piece of code and
want to see all the occurrences of a variable name.

hope that helps
raju
--
Kamaraju S Kusumanchi | http://raju.shoutwiki.com/wiki/Blog

Waters, Bill

unread,
Jan 17, 2017, 8:27:12 PM1/17/17
to vim...@googlegroups.com
Look at the SearchAlternatives plugin. When you add an item to the search list, it does not advance to the next match.
--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages