0

I want to enable vim-ag to search a word inside specific directory.

Currently I have the following mapping in my vimrc:

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

which searchs the cword in the current directory.

I want to expand it a bit so that I would be able to search in another directory as well.

I'm not sure if I can use noremap to get additional input (the target directory). If this is possible - then that would be best (i.e. press <leader>a -> add somehow the target directory -> Ag will search the cword in target directory)

If not - I want to config Ag manually - with another binding, to look in my git repo head directory - something like:

noremap <leader>a  :Ag! <C-r>=expand('<cword>') <GIT REPO DIR><CR><CR>
  1. would that work (I'm not sure if the expand operator breaks things or not
  2. how do I get the git repo dir?
2
  • 1
    1. It's "register", not "buffer". 2. You appear to be asking two different things: how to use the visually highlighted text in a command and how to make your plugin search in the working directory. Those are separate problems that you should ask about in separate questions.
    – romainl
    Feb 9 at 8:35
  • Thanks for the coment - I'll rephrase Feb 9 at 8:35

3 Answers 3

2

The right-hand-side of your mapping is a macro: a collection of keystroke that Vim performs for you. This means that, with the cursor on foo, your mapping "types" the following in the command-line:

Ag! foo

and immediately presses <CR>, executing the command.

If you want a chance to provide a directory, you can simply:

  • add a space after the first <CR>,

  • and remove the second <CR>, like this:

    nnoremap a :Ag! =expand('')

It will leave you there:

:Ag! foo
         ^

ready to type your directory. If you don't want to specify a directory, just press <CR>.

That said, the plugin seems to have an option https://github.com/rking/ag.vim#configuration, that might help with your underlying problem.

1

I would propose you to make sure the current working directory is the directory of the git root.

To achieve that automatically you could use the vim-rooter plugin.

1
  1. You can use input() to request input. Something like :nnoremap <leader>a :Ag! stuff <C-r>=input('directory')<CR><CR>
  2. If you’re already using fugitive, it provides FugitiveGitDir(); otherwise, something like systemlist('git rev-parse --git-dir')[0], I think.
1
  • I do use fugitive! I'll check it, thanks Feb 9 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.