VimPlugin: fzf.vim

From wikinotes

A very customizable fuzzy-searching plugin/framework for vim.

Documentation

github (fzf - main) https://github.com/junegunn/fzf
github (fzf.vim - utils) https://github.com/junegunn/fzf.vim
:h fzf https://github.com/junegunn/fzf.vim/blob/master/doc/fzf-vim.txt

Usage

Anatomy of a basic fzf.vim command

let l:options = {
    'source': 'find .',                          " (str) cli command to locate files, (list) items to display
    'sink': 'e',                                " when you've chosen a file, run this command (`:edit`)
    'sinklist': {arg -> s:some_func(arg)},      " alt, when you've chosen a file, run this lambda expression
    'dir:' '~',                                 " (opt) open relative to this dir
    '+s',                                       " don't let fzf sort results
    'options': { '-q', 'foo' },                 " pass fzf-cli options
}
call extend(l:options, fzf#vim#with_preview())  " use sane defaults for preview-showing
call fzf#run(l:options)                         " run fzf

Examples

call fzf#run(fzf#wrap({ source: 'git ls-files' }))   " fzf results of command 'ls-files'
call fzf#run(fzf#wrap({ source: ["abc", "def"] }))   " fzf from a list instead of cli-command

You can use the output from fzf in a function that gets called https://github.com/junegunn/fzf/issues/2056

If sink/sinklist is not specified, returning the chosen files (as a list) is the default behaviour.

let chosen_files = fzf#run({'source': 'find ~/.config' })  # capture chosen files

Cookbook

""" Redefine Rg command to allow rg arguments to pass through
""" Examples:
"""    :Rg -g "*.rb" "class Foo"   # cli params
"""    :Rg .                       # all lines in all files
"""
""" Notes:
"""     :Rg   (will not work anymore without params)
"""
"""
command! -bang -nargs=* Rg
  \ call fzf#vim#grep(
  \   'rg --column --line-number --no-heading --color=always --smart-case '.(<q-args>),
  \   1,
  \   <bang>0 ? fzf#vim#with_preview('up:60%') : fzf#vim#with_preview('right:50%:hidden', '?'),
  \   <bang>0)
""" Custom 'Rg' command.
""" Wraps regular rg arguments, and adds -s/-t to include only src or test files.
"""
""" Examples:
"""    :Rg -g "*.rb" "class Foo"   # cli params
"""    :Rg .                       # all lines in all files
"""    :Rg -s foo                  # search sourcefiles for 'foo'
"""    :Rg -t foo                  # search testfiles for 'foo'
"""
"""
function! s:fzf_rg_custom(...)
    let l:cmdargs = ['rg', '--column', '--line-number', '--no-heading', '--color=always', '--smart-case'] + a:000
    let l:cmd_suffix = ""

    if (index(l:cmdargs, '-s') >= 0)
        " let l:cmd_suffix = '| grep -v test'
        call add(l:cmdargs, "-g")
        call add(l:cmdargs, '!*test*')
        let l:cmdargs = filter(l:cmdargs, "v:val!='-s'")
    endif
    if (index(l:cmdargs, '-t') >= 0)
        " let l:cmd_suffix = '| grep test'
        call add(l:cmdargs, "-g")
        call add(l:cmdargs, '*test*')
        let l:cmdargs = filter(l:cmdargs, "v:val!='-t'")
    endif
    let l:cmdstr = join(map(l:cmdargs, 'shellescape(v:val)'), ' ')
    let l:cmdstr = l:cmdstr ." ".l:cmd_suffix

    call fzf#vim#grep(l:cmdstr, 1, fzf#vim#with_preview(), 0)
endfunc
command! -nargs=* Rg :call s:fzf_rg_custom(<f-args>)