DEV Community

Kay Gosho
Kay Gosho

Posted on

fzf.vim GFiles enhancement for monorepo

I'd like fzf.vim GFiles to add my current directory to initial query.

Before:

image

After:

image

I end up to create a custom command GFilesMonorepo. Here is the implementation:

" ported from https://github.com/junegunn/fzf.vim/blob/master/autoload/fzf/vim.vim#L546
function! s:get_git_root()
  let l:root = split(system('git rev-parse --show-toplevel'), '\n')[0]
  if v:shell_error
      return ''
  endif
  return l:root
endfunction

function! s:gitfiles_monorepo()
  let l:root = s:get_git_root()
  let l:path = substitute(getcwd(), l:root, '', '')
  let l:path = substitute(l:path, '/', '', '')

  let l:options = '-m --preview "head -20 {1}" --prompt "GitFiles> " '
  if l:path != ''
    let l:options .= '--query '.l:path
  endif

  call fzf#run({
  \ 'source':  'git ls-files | uniq',
  \ 'sink': 'e',
  \ 'dir': l:root,
  \ 'options': l:options,
  \ 'down':    '40%'
  \})
endfunction
command! GFilesMonorepo call s:gitfiles_monorepo()
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)