Search Vim with the word under the cursor

I would like to put <Leader>a on a search with ag word under the cursor

I wrote this:

 noremap <Leader>a :Ag!<Cu><Cr>=Escape(expand('<cword>'))<CR> function! Escape(stuff) return substitute(escape(a:stuff, '\/.*$^~[]'), "\n", '\\n', "g") endfunction 

Unfortunately, when I pressed <Leader>a on the word foo , I get the following:

 :foo 

Ag! disappeared and the final <CR> not executed.

Where is my mistake?

+6
source share
2 answers

you added <cu> to your mapping, it will remove :Ag!

You can use -Q for ag to execute literals.

For the <CR> problem, your <CR> is for the expression <cr>= , to run this command you need another <CR> .

+8
source
 noremap <leader>a :Ag! "<cword>"<cr> 
0
source

Source: https://habr.com/ru/post/984774/


All Articles