Search whole project by default with Vim/Ack
Asked Answered
P

4

6

I would like to use Ack (or similar plugin if something else can do the job) to search my whole project in Vim by default, rather than just the current directory. Ideally I'd end up with a process that works like using Cmd+Shift+F in Sublime. How can I do this?

An option like CtrlP's let g:ctrlp_working_path_mode = 'r' that makes it search within the nearest parent directory that contains a file like .git would be perfect. (https://github.com/kien/ctrlp.vim#basic-options)

Pronounced answered 15/10, 2013 at 9:49 Comment(3)
Usually, your project directory is your working directory. If that's not the case you are probably doing something wrong like… starting Vim from your $HOME and opening files from there.Kampala
Thanks for this - I'm using MacVim, which opens in $HOME and then navigating to my project from there using NERDTree - would that explain it? On the other hand, when I have a file open it seems to only search within that file's immediate parent directory.Pronounced
Navigate to your project and open MacVim from there, that's all there is to it. Without a link to your .vimrc it's hard to go further but it smells like a NERDTree option or autochdir.Kampala
U
2

I don't think Ack (or grep/vimgrep) can detect your "project root". If you often work on several projects, you could add this block in your vimrc:

let g:projectA_path="/path/to/A"
let g:projectB_path="/path/to/B"
let g:projectC_path="/path/to/C"

also define some functions/commands, like AckA, AckB, AckC... basically the func/command just does:

exec 'Ack! '. pattern . " " . g:projectA_path

the pattern is the argument you passed in. then, in future, you could do:

:AckA foo

or

:call AckA("foo")

for quick grepping/acking in projectA.

I didn't think of a simpler way to do it. glad to see if there is better solution.

Upholstery answered 15/10, 2013 at 10:9 Comment(0)
S
3

I think Rooter is what you want. For example:

let g:rooter_patterns = ['Rakefile', '.git/']
Show answered 15/10, 2013 at 21:1 Comment(0)
U
2

I don't think Ack (or grep/vimgrep) can detect your "project root". If you often work on several projects, you could add this block in your vimrc:

let g:projectA_path="/path/to/A"
let g:projectB_path="/path/to/B"
let g:projectC_path="/path/to/C"

also define some functions/commands, like AckA, AckB, AckC... basically the func/command just does:

exec 'Ack! '. pattern . " " . g:projectA_path

the pattern is the argument you passed in. then, in future, you could do:

:AckA foo

or

:call AckA("foo")

for quick grepping/acking in projectA.

I didn't think of a simpler way to do it. glad to see if there is better solution.

Upholstery answered 15/10, 2013 at 10:9 Comment(0)
M
1

Most of the time I don't need to cd the project root, but stay in the same working directory. So there is a simpler solution, based on answer of Kent, without cd'ing the project root, installing additional plugins and using ag:

let g:ackprg = 'ag --vimgrep --smart-case'

function! Find_git_root()
  return system('git rev-parse --show-toplevel 2> /dev/null')[:-2]
endfunction

command! -nargs=1 Ag execute "Ack! <args> " . Find_git_root()

And to use it call :Ag <keyword>

Mayne answered 2/9, 2018 at 15:41 Comment(1)
to get rid of the trailing line ending you can also pipe to tr -d '\\n' as exlained in this answerBiforked
B
0

I have this line in my .vimrc:

cnoreabbrev ack cd ~/your-project <bar> Ack! <Space>

Whenever you type :ack and hit the space the rest will be added to the command line and you can add the keyword.

Buttonhole answered 31/10, 2019 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.