Wednesday, June 30, 2010

Re: Analog to SHIFT+Asterisk

On Wed, 30 Jun 2010, Hoss wrote:

> Esteemed Vim Users,
>
> One of the useful key combinations in vim is shift+asterisk, which
> will locate the word your cursor is "on", and put that word into your
> "/ buffer (surrounded by \<\> word boundaries). This has the effect of
> highlighting all occurrences of that word.
>
> It also has the effect of jumping the cursor to the next instance of
> that word in your buffer.
>
> Is there an analogous key combination, that will highlight the current
> word, WITHOUT moving my cursor?

Fun:

:nmap A :call setreg('/','\<'.expand('<lt>cword>').'\>')<CR>

Replace 'A' with whatever key(s) you want to map it to.

That sets the search register to start-of-word + the current word +
end-of-word without actually performing the search. The search doesn't
show up in the search history (q/), though. The much-simpler:

:nmap A *<C-O>

will perform a star search (ha), and then go back to the prior position.
Has the advantage of putting it in the search history but (IMO,
significantly-worse) disadvantage of moving the cursor if the next match
is off-screen.

See:

:help key-mapping
:help :nmap
:help expand()
:help map.txt | /<lt> -- for why I used '<lt>' in the mapping
:help :<cword>

--
Best,
Ben

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

No comments:

Post a Comment