Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim search with ag the word under cursor

Tags:

vim

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

I wrote this:

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

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

Unfortunately when I hit <Leader>a on the word foo I get this:

:foo

The Ag! vanished and the trailing <CR> was not executed.

Where is my mistake?

like image 352
nowox Avatar asked Apr 07 '15 08:04

nowox


People also ask

How do I move the cursor to the end of a word in Vim?

Press e (“end”) to move the cursor to the last character of the current word.


2 Answers

you added <c-u> in your mapping, it will remove :Ag!

You may want to use -Q for ag to do a literal search.

For the <CR> problem, your <CR> is for the <c-r>= expression, you need one extra <CR> to launch the command.

like image 100
Kent Avatar answered Sep 28 '22 11:09

Kent


noremap <leader>a :Ag! "<cword>"<cr>
like image 38
Pegasus Avatar answered Sep 28 '22 12:09

Pegasus