junegunn / fzf.vim

fzf :heart: vim
MIT License
9.55k stars 583 forks source link

Proposal: Flag to change `dir` from cwd to project root #1415

Closed tlelson closed 1 year ago

tlelson commented 1 year ago

I have defined commands similar to the plugin default commands that use the git root as the dir argument. Example:

  command! -bang -nargs=* RG
    \ call fzf#vim#grep(
    \   "rg --column --line-number --no-heading --color=always --smart-case -- ".shellescape(<q-args>), 1,
    \   fzf#vim#with_preview({
    \     'dir': system('git -C '.expand('%:p:h').' rev-parse --show-toplevel 2> /dev/null')[:-2],
    \     'options': ['--prompt', 'RG> '],
    \   }), 
    \   <bang>0),

I don't want to redefine Rg because I find it very useful to search from the current buffers path just as often as searching the whole project.

I have a growing list of commands that closely shadow their original version.

I would like to provide an argument instead but I see that this would conflict with the underlying command's arguments.

I'd be happy to contribute a PR if it were deemed valuable and achievable in a way consistent with the rest of the project.

Would anyone else find it useful to easily switch the following commands between buffer cwd and git root? Rg, Files, GFiles ...

junegunn commented 1 year ago

I've never needed this because I always stay in the project root once I start Vim. I'm genuinely curious, is there any reason you switch directories?

Having said that, I'm not against adding an option. Need to put more thought into what would be the most convenient and flexible way to configure the behavior.

tlelson commented 1 year ago

This happens for me if I've followed code into a standard or third party library package somewhere. It also happens if I've opened up some reference code/document or opened vim configurations or plugin code.

Perhaps its only relevant to those using autochdir since that will localize any actions to the buffer's location.

michaelorr commented 1 year ago

@tlelson I have the same use case as you and I have addressed it via: https://github.com/airblade/vim-rooter

By setting vim's working directory to the project root, you get the added benefit of other cwd-centric tools working as expected also.

tlelson commented 1 year ago

Perhaps it betrays my low maturity level but I haven't been able to visit the project without a chuckle. Nonetheless, a smarter version of autochdir is what I'm looking for. Thanks @michaelorr I shall give it a go.

MimmyJau commented 1 year ago

Initially, I came in here looking for an equivalent to :GFiles for :Rg (grepping any file in the current repo). But @junegunn's remark about immediately starting vim in my project's root directory has me questioning my workflow (and for now serves as a pretty good solution to my problem).