Vim 用 ag 光标下的词搜索

Vim search with ag the word under cursor

我想映射 <Leader>a 以搜索 ag 光标下的词

我写了这个:

noremap <Leader>a  :Ag!<C-u><C-r>=Escape(expand('<cword>'))<CR>

function! Escape(stuff)
    return substitute(escape(a:stuff, '\/.*$^~[]'), "\n", '\n', "g")
endfunction

不幸的是,当我在 foo 这个词上点击 <Leader>a 时,我得到了这个:

:foo

Ag! 消失了,尾随的 <CR> 没有被执行。

我的错误在哪里?

您在映射中添加了 <c-u>,它将删除 :Ag!

您可能想对 ag 使用 -Q 进行文字搜索。

对于 <CR> 问题,您的 <CR> 是针对 <c-r>= 表达式的,您需要一个额外的 <CR> 来启动命令。

noremap <leader>a :Ag! "<cword>"<cr>

添加我在 GitHub 中找到的答案 junegunn 以备不时之需

nnoremap <silent> <Leader>ag :Ag <C-R><C-W><CR>

Original link