Search code examples
vimvim-pluginmruneovimfzf

Working solution for FZF most recent files in Vim?


I have a problem with finding a proper solution for most recently used files with the FZF Vim plugin.

This plugin should have features like:

  • show files opened in current vim session(like buffers)
  • filter files like NERD_tree, fugitive

I tried two solutions

command! FZFMru call fzf#run({
\ 'source':  reverse(s:all_files()),
\ 'sink':    'edit',
\ 'options': '-m --no-sort -x',
\ 'down':    '40%' })

function! s:all_files()
  return extend(
  \ filter(copy(v:oldfiles),
  \        "v:val !~ 'fugitive:\\|\\.svg|NERD_tree\\|^/tmp/\\|.git/'"),
  \ map(filter(range(1, bufnr('$')), 'buflisted(v:val)'), 'bufname(v:val)'))
endfunction

This one works during open session but when I restart Vim I don't see all last opened files.

command! FZFMru call s:fzf_wrap({
    \'source':  'bash -c "'.
    \               'echo -e \"'.s:old_files().'\";'.
    \               'ag -l -g \"\"'.
    \           '"',
    \})

function! s:fzf_wrap(dict)
    let defaults = {
    \'sink' : 'edit',
    \'options' : '+s -e -m',
    \'tmux_height': '40%',
    \}
    call extend(a:dict, defaults, 'keep')
    call fzf#run(a:dict)
endfunction

function! s:old_files()
    let oldfiles = copy(v:oldfiles)
    call filter(oldfiles, 'v:val !~ "fugitive"')
    call filter(oldfiles, 'v:val !~ "NERD_tree"')
    call filter(oldfiles, 'v:val !~ "^/tmp/"')
    call filter(oldfiles, 'v:val !~ ".git/"')
    call filter(oldfiles, 'v:val !~ ".svg"')
    return join(oldfiles, '\n')
endfunction

This solution filters files properly but works only for files opened in previous session. So I need to restart Vim to get the current buffer.

Did you find a working solution for FZFMru in Vim?


Solution

  • I found a latest Junegunn plugin.

    Plug 'junegunn/fzf.vim'
    

    It covers a case.

    Just add

    nmap <silent> <leader>m :History<CR>
    

    Thanks Junegunn :)