Re:A Vim Guide for Advanced Users
by Skunkleton on Saturday February 27, 2021 @03:28PM
(#26287865)
This might be the right audience for this little vimrc tidbit:
"Press space to highlight current word
nnoremap <silent><space>
\ :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
This works just like *, except that it leaves your cursor where it is. You can then use n/p/whatever as normal.> like * but leaves your cursor where it is
Ooooh that is great! I use *# every time and am then annoyed when the n key goes backwards instead of forwards and then I try to remember to do #* instead and pff... ^^ This will solve those issues!
This is useful enough that I wonder why you chose not to just map it to *. Is there a reason you did not?
Re:A Vim Guide for Advanced Users
by Skunkleton on Saturday February 27, 2021 @07:18PM
(#26289471)
Because <space> is even easier to hit :)
Edit: also because I try and keep my keybinds as close to default as I can, and <space> is oddly underused.
Re:A Vim Guide for Advanced Users
by CGamesPlay on Saturday February 27, 2021 @11:32PM
(#26290691)
> But perhaps it's a bug in Vim since it works when I run * on the lowercase variant.
Per the help it's a feature, and it's infuriating! (From :help star)
> 'ignorecase' is used, 'smartcase' is not.
Here's your workaround:
" By default, * and # respect 'ignorecase'. This mapping forces these commands
" to search case sensitively, which is usually what you want when searching
" code with * or #. May be less good for prose.
nnoremap <silent> * /\C\<<C-R>=expand('<cword>')<CR>\><CR>
nnoremap <silent> # ?\C\<<C-R>=expand('<cword>')<CR>\><CR>
It also has the downside that it doesn't match what is being shown: when I hit * on "overview" it highlights both "overview" and "Overview" (I have highlight search enabled), but cgn replaces only one of them. When the cursor is on Overview and I run "*cgnexample<esc>..." it will skip all the "Overview"s and only replace "overview".
But perhaps it's a bug in Vim since it works when I run * on the lowercase variant.