lotabout / skim.vim

vim support for skim
132 stars 19 forks source link

Skim != FZF with fzf#vim#grep custom command for path specific fuzzy search #18

Open casonadams opened 3 years ago

casonadams commented 3 years ago

The following command does a rg find in my $NOTES dir and works as expected using fzf. The exact same command using skim doesn't seem to respect the rg commands.

"------------------------------------------------
" Note search START
command! -bang -nargs=* Notes
\ call fzf#vim#grep(
\   "rg
\     --column
\     --line-number
\     --no-heading
\     --color=always
\     --smart-case
\     --passthru
\     ''
\     $NOTES
\   ".shellescape(<q-args>),
\   1,
\   fzf#vim#with_preview(),
\   <bang>0
\ )
"------------------------------------------------
ranelpadon commented 3 years ago

@lotabout I have similar issue. And I have lots of customization for rg and rg + fzf.vim, but it doesn't work with skim.vim. For example, I want to search specific file types only like Python files only (rg --type py) or JS files only (rg --type js). This is useful/practical since it speeds up the search process especially for huge codebase and eliminates noise (that is, excludes file types you don't care about). So, basically I just want to pass some custom args to the rg command (e.g. --type). Similarly, the OP is passing some custom args also.

This is surprisingly easy to replicate though. This sample code in the README doesn't work, at least on my end:

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case -- %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let spec = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(spec), a:fullscreen)
endfunction

command! -nargs=* -bang RG call RipgrepFzf(<q-args>, <bang>0)

So, when you type :RG, there will be a floating terminal for a very brief moment, then it will vanish. I'm guessing that the plugin couldn't process the command, and just exits.

And ultimately, I just want to add something to base rg command above, like: let command_fmt = 'rg --type js ...

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --type py ...'
  ...
endfunction

Also, it seems not possible to pass additional args to this core skim function?

command! -bang -nargs=* Rg call fzf#vim#rg_interactive(<q-args>, fzf#vim#with_preview('right:50%:hidden', 'alt-h'))

Are we missing something?

BTW, thanks for this interesting/promising plugin. :)

ranelpadon commented 3 years ago

From the source code, fzf#vim#rg_interactive has hardcoded command indeed:

function! fzf#vim#rg_interactive(dir, ...)
  let dir = empty(a:dir) ? '.' : a:dir
  let command = 'rg --column --line-number --color=always '.get(g:, 'rg_opts', '').' "{}" ' . dir
  return call('fzf#vim#grep_interactive', extend([command, 1], a:000))
endfunction

So, couldn't pass additional rg arg dynamically.

ranelpadon commented 3 years ago

So, a workaround to pass additional rg arg is to define a similar function:

" Grep the Python files only, exclude the unit tests folder/files.
function! RgPy(dir, ...)
  let dir = empty(a:dir) ? '.' : a:dir
  let command = 'rg --type py --glob "!**/tests/**" --column --line-number --color=always '.get(g:, 'rg_opts', '').' "{}" ' . dir
  return call('fzf#vim#grep_interactive', extend([command, 1], a:000))
endfunction

command! -nargs=* -bang RgPy call RgPy(<q-args>, fzf#vim#with_preview('right:60%'))