mileszs / ack.vim

Vim plugin for the Perl module / CLI script 'ack'
Other
3.08k stars 396 forks source link

Search in 'project root' instead of working directory #188

Closed rrbarbosa closed 8 years ago

rrbarbosa commented 8 years ago

Similar projects have an option to follow .git, .hg and etc to find the 'project root' and then perform the searches from that directory. That was the main reason for me to use to ag.vim instead of ack.vim, but now the former is dead.

Is this something that could/will be implemented in ack.vim?

ches commented 8 years ago

Hi,

Could you describe some of your use cases where this would help and you're not getting the behavior you want or expect?

My gut feeling is that a notion of a "project root" is probably out of scope for this plugin, Vim's existing functionality around cd and lcd and potential for automating them with autocommands, etc. should be able to cover it in a general way. Something like vim-projectionist and its :ProjectDo command might give you higher-level functionality wrapping that out of the box.

With my Vim usage habits :pwd is generally always my project root already, so maybe I'm not understanding your situation.

rrbarbosa commented 8 years ago

I often have vim working folder to be the folder of the file I am editing. This way I get a very convenient file name completion with the default ctrl-x ctrl-f.

Another example is browsing a large code base from the terminal, jumping into a source file with vim and then trying to find other places a function is used in the code base. If I know the name of the file, I can jump using [ctrlp.vim](https://github.com/ctrlpvim/ctrlp.vim), which detects the "project root", but if I don't, I need to manually jump a few folders up when calling ack.vim.

Automation is certainly a solution for frequently used code bases, but it's not practical when exploring new ones.

ches commented 8 years ago

Thanks for your additional explanation.

I'm still not convinced it's the search plugin's place to have or need an explicit understanding of this. The situations you mention seem exactly where :cd or :lcd are designed to be used; exploring an unfamiliar code base is IMO the best use of netrw, Dirvish, NERDTree, or the like (those plugins often have a feature for using :cd conveniently, too). Or additionally/better yet, ctags!

I'm not saying "your workflow is wrong" for exploring directory structure via the shell, but I think it is probably the minority case, much as 'autochdir' is pretty exotic because it is often more disruptive than helpful.

Aside from Projectionist, it might also be useful to know that VCS plugins often have root-related cd features—Fugitive and Lawrencium have :Gcd and :Hgcd, for instance.

:vimgrep and :grep work relative to :pwd as well, which is one of my litmus tests for features when following their behavior seems sensible. Finally, what should the default be if a project root notion was supported? For everyone who wants searches to run from the root by default, someone else will have a reason to wish it was from :pwd instead. Needing a configuration option and/or possibly a toggle for this is a further reason it doesn't appeal to me when there are many other general, often built-in ways to skin this cat.

rrbarbosa commented 8 years ago

Guess I will just have to give ctrlsf.vim and Fugiviteve'sGgrep a go. All falling, I can always try to implement the feature myself.

sblask commented 7 years ago

I had created an alias for Ack anyway (cnoreabbrev ag Ack!), so it was easy to extend it to get the desired behaviour, just add the following to your vimrc:

cnoreabbrev ag Gcd <bar> Ack!

Then every time you type :ag and space, it will get extended to :Gcd | Ack! which changes the working directory to the folder containing .git (see https://github.com/tpope/vim-fugitive/blob/master/doc/fugitive.txt) and then do the search. As my main reason for this was using autochdir, changing the working directory automatically is not problem - next time I change to another buffer, the working directory is back to where it belongs...

toejough commented 7 years ago

I understand the argument against putting this behavior into the plugin, but for those of us who do use autochdir, or just for people who jump right into a file from eleswhere instead of cd'ing to project root then opening a file, having a way to search from the project root without issuing extra commands is super handy.

Would it be possible to put @sblask's solution or something similar into a FAQ?

n10v commented 6 years ago

Most of the time I don't need to cd the project root, but stay in the same working directory. So I found one more simpler solution without cd'ing the project root and installing additional plugins:

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>