Working solution for FZF most recent files in Vim?
Asked Answered
S

3

30

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?

Sub answered 28/7, 2015 at 14:50 Comment(0)
S
41

I found a latest Junegunn plugin.

Plug 'junegunn/fzf.vim'

It covers a case.

Just add

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

Thanks Junegunn :)

Sub answered 17/8, 2015 at 9:48 Comment(3)
appears this doesn't work (at least anymore). "Not an editor command: History"Duplicity
Works for me with nvim 0.2.2 and the fzf plugins up to date.Saiz
@Duplicity Most probably you've forgotten to install "Plug 'junegunn/fzf.vim'".Anaerobe
D
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>
Dahna answered 17/8, 2015 at 9:23 Comment(1)
works as expected. Thanks. Just not sure if we need unite support here :)Sub
P
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%'})
Pipit answered 6/12, 2017 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.