fzf to read file into nvim buffer

688 views Asked by At

I have a directory with a group of templates used for technical writing and shared with other people (they use it as part of a sublime package). Since I use nvim, I will often open a separate tmux pane, use :Files from fzf.vim to open the template, copy its content, and paste it into my buffer.

Alternatively, I can read :r from the template directory, but that doesn't come with a fuzzy search, making it hard to find the right file among hundreds of options. gotbletu's https://www.youtube.com/watch?v=Zew0mgJwAh8 would be a perfect solution, except that I can't add the tags on top of each template that his script requires (again, the templates are shared with other people).

I've tried to combine :r with fzf in a shell function, but so far nothing works. Any thoughts on how to get this done?

1

There are 1 answers

1
sudavid4 On

you may try putting something like the following in your init.vim

let g:pathToTemplates='/tmp/'
function! GoSink(file)
    execute ':r '.g:pathToTemplates.a:file
endfunction
command! Go call fzf#run({
    \  'source': 'ls '.g:pathToTemplates,
    \  'sink':    function('GoSink')})

then just type :Go

if you really want to do this from the terminal then you'll want this in your bashrc/zshrc/...

function fzfreadtemplate(){
    local templatedir='/tmp/'
    filename="$templatedir"$(ls "$templatedir" | fzf)
    if [[ -f "$filename" ]]; then
        vim -c ':r '"$filename"
    fi
}