vim:视觉明星搜索未按预期工作

2024-04-28

我复制了这个函数来进行视觉搜索* and #:

function! s:VSetSearch(cmdtype)
  let temp = @s
  norm! gv"sy
  let @/ = '\V' . substitute(escape(@s, a:cmdtype.'\'), '\n', '\\n', 'g')
  let @s = temp
endfunction

xnoremap * :<C-u>call <SID>VSetSearch('/')<CR>/<C-R>=@/<CR><CR>
xnoremap # :<C-u>call <SID>VSetSearch('?')<CR>?<C-R>=@/<CR><CR>

The #映射工作正常,但*映射不会退出视觉选择(它扩展视觉选择的范围,直到下一个搜索词)。我不明白为什么会发生这种情况。有解决办法吗?

EDIT:要重现问题,请保存代码片段,下载微软安装程序 ftp://ftp.vim.org/pub/vim/pc/gvim74.exe, open cmd.exe并启动 vimvim -u NONE, 然后做:set nocp最后获取保存的代码。事实上,下面的简单映射也不起作用:

nnoremap * *<C-o>

EDIT 2:其他人可以重现这个问题吗?是否应该报告?

EDIT 3:我相信问题(错误?)是*(星号)键无法重新映射:如果我用以下命令启动 vimvim -N -u NONE(Vim 7.4,带有补丁 1-274)并运行命令:noremap * :echo "star"<CR>并按*,vim 尝试执行搜索。我也举报了this https://groups.google.com/forum/#!topic/vim_dev/nkcV51bsQuc到 vim 开发组。


以下是我使用的:

function! s:Vword()
  return getline('.')[col("'<")-1:col("'>")-1]
endfunction

xnoremap <silent> * <Esc>/\v<<C-R>=<SID>Vword()<CR>><CR>
xnoremap <silent> g* <Esc>/\v<C-R>=<SID>Vword()<CR><CR>

xnoremap <silent> # o<Esc>?\v<<C-R>=<SID>Vword()<CR>><CR>
xnoremap <silent> g# o<Esc>?\v<C-R>=<SID>Vword()<CR><CR>

nnoremap <silent> g// :grep -w <cword> <C-R>=getcwd()<CR><CR>
nnoremap <silent> g/* :grep <cword> <C-R>=getcwd()<CR><CR>

xnoremap <silent> g// :<C-U>grep -w <C-R>=<SID>Vword()<CR> <C-R>=getcwd()<CR><CR>
xnoremap <silent> g/* :<C-U>grep <C-R>=<SID>Vword()<CR> <C-R>=getcwd()<CR><CR>

I have also additionally added nice mappings for g* & similarly g# and also a bunch of mappings for invoking grep that I find very useful.

编辑:对代码进行小修正。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vim:视觉明星搜索未按预期工作 的相关文章

随机推荐