mihaifm / vimpanel

A modern side panel for Vim
106 stars 9 forks source link

Search in multiple directories? #14

Open eduardoarandah opened 5 years ago

eduardoarandah commented 5 years ago

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

Seems that CtrlP doesn't support that https://github.com/ctrlpvim/ctrlp.vim/issues/436

NerdTree doesn't do it https://github.com/scrooloose/nerdtree/issues/215

My workflow always involves jumping from one directory to another.

Do you know a way of ?

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Yes! it worked!

Caveats:

1) I had to remove caching

let g:ctrlp_use_caching = 0

2) 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 commented 5 years ago

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 commented 5 years ago

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:

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 commented 5 years ago

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 commented 5 years ago

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