ViM:搜索并突出显示但不跳转
ViM中的超级星号(*)键将在光标下搜索单词,并向前跳至下一个匹配项。 用户可以使用n键跳至下一场比赛。 如果启用了hlsearch,则还将突出显示匹配项。
我希望能够按*并获得突出显示的匹配项,并能够使用n键浏览匹配项。 但是,我不希望ViM在按*时跳到下一个匹配项,它应该保留在当前单词上。 有没有办法做到这一点?
Ashwin Nanjappa asked 2020-08-05T14:38:20Z
11个解决方案
26 votes
我会映射:
nnoremap * *``
除了在跳转列表中添加一个跳转外,其他都与您想要的完全一样。 为避免这种情况,您需要:
nnoremap * :keepjumps normal *``
mb14 answered 2020-08-05T14:38:31Z
6 votes
最佳解决方案:
不要将跳转添加到跳转列表中
星形键的行为不会改变
因此,请尝试使用插件:[http://www.vim.org/scripts/script.php?script_id=4335]
远胜于:
" a jump adds to the jump list
nnoremap * *``
" I got a dead loop on macvim
nnoremap * :keepjumps normal *``
" the behavior is changed
nnoremap * :let @/='\<=expand("")\>':set hls
name5566 answered 2020-08-05T14:39:09Z
5 votes
我的.vimrc文件中包含以下内容,我认为它比其他替代方法更好:
" Put word under cursor into search register and highlight
nnoremap * :let @/='\<=expand("")\>':set hls
vnoremap * :
\let old_reg=getreg('"')let old_regtype=getregtype('"')
\gvy:let @/=substitute(
\escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')
\gV:call setreg('"', old_reg, old_regtype):set hls
Jeet answered 2020-08-05T14:39:29Z
5 votes
如果要保留当前视图并将搜索添加到历史记录中,请尝试此[效率不高]解决方案:
noremap * msHmt`s*`tzt`s
它使用标记s(保存)和t(顶部)。
Rodrigo Farias answered 2020-08-05T14:39:53Z
3 votes
我想到一个简单的解决方案:将map * *#放在.vimrc文件中(尽管它将闪烁)。
khachik answered 2020-08-05T14:40:14Z
3 votes
我还没有看过这个:
s
它很短,不涉及跳动,可能会导致眨眼。
说明:将光标下的单词复制到s寄存器,然后将搜索寄存器(/)设置为s寄存器的内容。 搜索寄存器不能直接写,这就是为什么必须让let并因此silent来保持vim的命令行干净的原因。
Sebastian Blask answered 2020-08-05T14:40:43Z
1 votes
我发现这很好用,没有眨眼,也不需要中间寄存器。
nnoremap * :let @/= '\') . '\>' set hls
或者,如果您想要g*的行为:
nnoremap g* :let @/=expand('') set hls
Uri answered 2020-08-05T14:41:08Z
1 votes
这里的许多答案都概述了相当简单的映射,这些映射在常见情况下效果很好,但是可能会产生副作用(例如,通过来回跳动而闪烁)或缺乏鲁棒性(如果将某些正则表达式字符定义为关键字字符,则可能会中断)。
如果您正在寻找可靠的实现,并且不介意安装插件,则可以从多种选择中进行选择,其中许多还提供与搜索相关的其他改进:
我的SearchHighlighting插件更改了z*命令,将其扩展为可视选择,并提供了可选的自动搜索光标下方单词的功能。
星形搜索将z*的行为更改为不跳至下一个匹配,并包括来自下一个插件的视觉搜索
vim-visual-star-search提供视觉选择的搜索
visualstar.vim提供了对视觉选择的搜索
选择和搜索可以在视觉选择中使用z*或*,并且可以避免跳跃。
vim-asterisk提供了一个z*映射,该映射也不会跳转,可视化*,更直观的智能案例处理,并且可以在跳转时保持光标位置(例如,*)
searchant.vim挂接到内置的搜索命令中,并为上次跳转到的比赛提供单独的突出显示。
Ingo Karkat answered 2020-08-05T14:42:08Z
0 votes
此处的其他答案很好,尤其是@rodrigo,但我想写一个保留滚动位置并且不影响任何标记的解决方案。
这对我有用:
function! StarPositionSave()
let g:star_position_cursor = getpos('.')
normal! H
let g:star_position_top = getpos('.')
call setpos('.', g:star_position_cursor)
endfunction
function! StarPositionRestore()
call setpos('.', g:star_position_top)
normal! zt
call setpos('.', g:star_position_cursor)
endfunction
nnoremap * :call StarPositionSave()*:call StarPositionRestore()
直接将normal! *放到函数中似乎不起作用,因为(至少在neovim中)它抑制了搜索突出显示的触发(就像运行了:nohlsearch一样)。
Sophie Alpert answered 2020-08-05T14:42:38Z
0 votes
我的解决方案:
nnoremap * v:count ? '*'
\ : ':execute "keepjumps normal! *" call winrestview(' . string(winsaveview()) . ')'
nnoremap g* v:count ? 'g*'
\ : ':execute "keepjumps normal! g*" call winrestview(' . string(winsaveview()) . ')'
优点:
没有闪烁。
跳转列表保持不变。
如果给出计数,则其作用类似于原始的*。
它不使用标记或寄存器。
使用实际的*,其行为与*几乎相同(除了跳跃)。
它不需要插件安装。
Bakudankun answered 2020-08-05T14:43:28Z
0 votes
类似于*我们有
[I ..................... it shows where the word under the cursor appears
我在vimrc上也有一些有用的行,可以帮助您
" When double click a word vim will hightlight all other ocurences
" see CountWordFunction()
" [I shows lines with word under the cursor
nnoremap <2-LeftMouse> :let @/='\V\'), '\').'\>':set hls:CountWord
nnoremap * :let @/='\V\'), '\').'\>':set hls:CountWord
if !exists('*CountWordFunction')
fun! CountWordFunction()
try
let l:win_view = winsaveview()
exec "%s/" . expand("") . "//gn"
finally
call winrestview(l:win_view)
endtry
endfun
endif
command! -nargs=0 CountWord :call CountWordFunction()
cnoreabbrev cw CountWord
nnoremap :CountWord
SergioAraujo answered 2020-08-05T14:43:53Z