29

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?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
tomekfranek
  • 6,852
  • 8
  • 45
  • 80

3 Answers3

40

I found a latest Junegunn plugin.

Plug 'junegunn/fzf.vim'

It covers a case.

Just add

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

Thanks Junegunn :)

tomekfranek
  • 6,852
  • 8
  • 45
  • 80
9

One possible solution is to leverage the neomru plugin which will save your most recently visted files to a cache located at ~/.cache/neomru/file.

After installing the neomru plugin with your preferred plugin manager, you can define a mapping to search the cache file, for example:

nnoremap <silent> <Leader>m :call fzf#run({
\   'source': 'sed "1d" $HOME/.cache/neomru/file',
\   'sink': 'e '
\ })<CR>
cjhveal
  • 5,668
  • 2
  • 28
  • 38
2

Check out https://github.com/junegunn/fzf/wiki/Examples-(vim). Lots of cool stuff there, including MRU, tags search, and much more. Junegunn implemented MRU simply as:

command! FZFMru call fzf#run({
\  'source':  v:oldfiles,
\  'sink':    'e',
\  'options': '-m -x +s',
\  'down':    '40%'})
verboze
  • 419
  • 1
  • 7
  • 10