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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Slartibartfast
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.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Tim Chase
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



--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Slartibartfast
>   :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.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Tim Chase
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



--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

kamaraju kusumanchi
In reply to this post by Slartibartfast
On Wed, Dec 14, 2016 at 4:03 AM, Slartibartfast
<[hidden email]> wrote:
> 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.
>

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

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Waters, Bill
In reply to this post by Slartibartfast
Look at the SearchAlternatives plugin.  When you add an item to the search list, it does not advance to the next match.


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Slartibartfast
Sent: Wednesday, December 14, 2016 1:03 AM
To: vim_use
Subject: How to prevent asterisk key from advancing to next match when pressed

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.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...