Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search in multiple directories? #14

Open
eduardoarandah opened this issue May 21, 2019 · 8 comments
Open

Search in multiple directories? #14

eduardoarandah opened this issue May 21, 2019 · 8 comments

Comments

@eduardoarandah
Copy link

I've been wondering how to quickly search in multiple directories like sublimetext.

Seems that CtrlP doesn't support that
ctrlpvim/ctrlp.vim#436

NerdTree doesn't do it
preservim/nerdtree#215

My workflow always involves jumping from one directory to another.

Do you know a way of ?

  • Provide a folder list
  • Search with RipGrep in every directory
  • Merge those results
  • Give the list back to CtrlP

This Setting let me to it with one directory, but I wonder if there's a way of accomplish the same thing merging results in all open directories:

Plug 'kien/ctrlp.vim'
let g:ctrlp_show_hidden = 1

" https://github.com/BurntSushi/ripgrep
if executable('rg')
    let g:ctrlp_user_command = 'rg --files %s'
    let g:ctrlp_use_caching = 1
    let g:ctrlp_working_path_mode = 'ra'
    let g:ctrlp_switch_buffer = 'et'
endif

@mihaifm
Copy link
Owner

mihaifm commented May 22, 2019

It's not too hard to iterate over the directories you've added in vimpanel. There is a g:VimpanelRoots global variable that holds all the paths.

However I'm not sure how you can integrate this with CtrlP. You need to change that rg command to search in all the folders. It can probably be done with a shell command, something along this line:

find PATH1 PATH2 PATH3 -type f | xargs rg --files

The vim script should be something like:

let g:MyGlobalSearchPath = ''

for root in g:VimpanelRoots
    let g:MyGlobalSearchPath = g:MyGlobalSearchPath . ' ' . root
endfor
        
if executable('rg')
    let g:ctrlp_user_command = 'find ' . g:MyGlobalSearchPath . ' -type f | xargs rg --files'
    let g:ctrlp_use_caching = 1
    let g:ctrlp_working_path_mode = 'ra'
    let g:ctrlp_switch_buffer = 'et'
endif

This is untested code, not sure if it will work but it could be a starting point.

@eduardoarandah
Copy link
Author

Hey!!
That's great news!

Actually, the only code CtrlP needs is something like

rg --files dir1 dir2 dir3

I tried this one but got "undefined g:VimpanelRoots"
I guess CtrlP loads the command on vim load

any ideas?

Plug 'kien/ctrlp.vim'
let g:ctrlp_show_hidden = 1

" https://github.com/BurntSushi/ripgrep
if executable('rg')

    let g:MyGlobalSearchPath = '' 
    for root in g:VimpanelRoots
        let g:MyGlobalSearchPath = g:MyGlobalSearchPath . ' ' . root
    endfor 

    let g:ctrlp_user_command = 'rg --files ' . g:MyGlobalSearchPath
    let g:ctrlp_use_caching = 1
    let g:ctrlp_working_path_mode = 'ra'
    let g:ctrlp_switch_buffer = 'et'
endif

@mihaifm
Copy link
Owner

mihaifm commented May 22, 2019

You're right...
Not to mention that the paths are dynamic, they can change when you add or remove folders to the panel.

Hmm, the way I see it there are two options. First is to make the above script a function, and call it via a keyboard mapping whenever you need to search something, but that would be a little cumbersome. Second option is for me to update the plugin, and provide some kind of callback functionality, where the plugin executes a user specified function when g:VimpanelRoots changes.

This would be the code for the first option (again, untested), with the F12 keyboard shortcut.

function! SetCtrlPUserCommand()
    if executable('rg')
        let g:MyGlobalSearchPath = '' 
        for root in g:VimpanelRoots
            let g:MyGlobalSearchPath = g:MyGlobalSearchPath . ' ' . root
        endfor 

        let g:ctrlp_user_command = 'rg --files ' . g:MyGlobalSearchPath
    endif
endfunction

nmap <silent> <F12> :call SetCtrlPUserCommand()<CR>

See if this works first, and then we'll see if I can update the plugin. Haven't coded in vimscript in years, need to get back in shape :)

@eduardoarandah
Copy link
Author

Yes! it worked!

Caveats:

  1. I had to remove caching
let g:ctrlp_use_caching = 0
  1. For some reason, ( in windows ) I have to add some dummy command inside.
    look at echo %s && rg --files
    I don't know why..
    some kind of validaton looking for %s ? no idea...

imagen

@mihaifm
Copy link
Owner

mihaifm commented May 22, 2019

Nice! Great job with that.

No clue about the %s stuff, maybe there's some weird logic inside CtrlP where it discards the command if it doesn't see the %s token...who knows. Anyway, good job of figuring it out.

I'll investigate adding a callback mechanism to the plugin, to avoid the need of a keyboard mapping.
Also, if you have any other feedback regarding this plugin let me know.

@eduardoarandah
Copy link
Author

Awesome!

Well, if it helps, my typical workflow is:

Having one project folder (with passwords, logins and stuff in .txt files) and one or more code folders

In code folder, typically:

  • F6 to toggle sidebar
  • F7 to "locate file" in sidebar
  • Some command to duplicate current file

Ctrlp +rg to fuzzy search everything

Vim Startify gives me session loading, saving and auto-persistence

So, in summary, these 3 plugins plus RipGrep provide same functionality as SublimeText or VSCode

VimPanel + CtrlP + VimStartify

@mihaifm
Copy link
Owner

mihaifm commented May 24, 2019

Ok, so I added g:VimpanelCallback(), a function that you can implement in your vimrc. It is executed every time an entry is added or removed from the panel, also when you refresh the panel (by pressing F5 inside Vimpanel).

Your code should look like this:

function! g:VimpanelCallback()
    if executable('rg')
        let g:MyGlobalSearchPath = '' 
        for root in g:VimpanelRoots
            let g:MyGlobalSearchPath = g:MyGlobalSearchPath . ' ' . root
        endfor 

        let g:ctrlp_user_command = 'echo %s && rg --files' . g:MyGlobalSearchPath
    endif
endfunction

Let me know when you manage to test this, and if it solves your problem.

thanks

@mihaifm
Copy link
Owner

mihaifm commented May 24, 2019

I think I should also run the callback when the panel is loaded. I'll make the change tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants