VimPlugin: fzf.vim: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 1: Line 1:
A very customizable fuzzy-searching plugin/framework for vim.
A very customizable fuzzy-searching plugin/framework for vim.


= Useful Commands =
= Documentation =
<blockquote>
{| class="wikitable"
|-
| github || https://github.com/junegunn/fzf.vim
|-
| <code>:h fzf</code> || https://github.com/junegunn/fzf.vim/blob/master/doc/fzf-vim.txt
|}
</blockquote><!-- Documentation -->
 
= Usage =
<blockquote>
Anatomy of a basic fzf.vim command
<syntaxhighlight lang="vim">
let l:options = {
    'source': 'find .',                        " cli command to locate files
    'sink': 'e',                                " when you've chosen a file, run this command (`:edit`)
    '+s',                                      " don't let fzf sort results
}
call extend(l:options, fzf#vim#with_preview())  " use sane defaults for preview-showing
call fzf#run(l:options)                        " run fzf
</syntaxhighlight>
</blockquote><!-- Usage -->
 
= Cookbook =
<blockquote>
<blockquote>
<source lang="vim">
<source lang="vim">
Line 55: Line 79:
command! -nargs=* Rg :call s:fzf_rg_custom(<f-args>)
command! -nargs=* Rg :call s:fzf_rg_custom(<f-args>)
</source>
</source>
</blockquote><!-- useful commands -->
</blockquote><!-- Cookbook -->

Revision as of 23:43, 18 February 2023

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

Documentation

github 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 .',                         " cli command to locate files
    'sink': 'e',                                " when you've chosen a file, run this command (`:edit`)
    '+s',                                       " don't let fzf sort results
}
call extend(l:options, fzf#vim#with_preview())  " use sane defaults for preview-showing
call fzf#run(l:options)                         " run fzf

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>)