4

例如,我想将其复制hello/world并粘贴到搜索其他实例中hello/world

我试过拉,然后/\v ctr-r 0在搜索栏中输入并放置整个hello/world但实际上,它只搜索hello.

有没有办法在搜索粘贴期间自动转义特殊字符?

4

2 回答 2

4

您可能对此感兴趣:

" return a representation of the selected text
" suitable for use as a search pattern
function GetVisualSelection()
  let old_reg = @a
  normal! gv"ay
  let raw_search = @a
  let @a = old_reg
  return substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction

xnoremap <leader>r :<C-u>%s/<C-r>=GetVisualSelection()<CR>/

它使用或多或少与上述相同的策略,但以更清洁和可重用的方式。

  1. 选择hello/world
  2. <leader>r
  3. 命令行已填充:%s/hello\/world/,可供您...</li>
  4. 输入替换文本,然后……</li>
  5. <CR>
于 2013-10-03T10:25:13.273 回答
0

将此添加到 vimrc 文件中,您可以直观地选择某些内容,按 *,它将使用转义字符进行搜索

vnoremap <silent> * :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy/<C-R><C-R>=substitute(
  \escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy?<C-R><C-R>=substitute(
  \escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>

资源

于 2013-10-03T10:06:46.713 回答