Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 713
tomekfranek Avatar asked Jul 28 '15 14:07

tomekfranek


3 Answers

I found a latest Junegunn plugin.

Plug 'junegunn/fzf.vim' 

It covers a case.

Just add

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

Thanks Junegunn :)

like image 139
tomekfranek Avatar answered Sep 21 '22 11:09

tomekfranek


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> 
like image 37
cjhveal Avatar answered Sep 19 '22 11:09

cjhveal


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%'})
like image 38
verboze Avatar answered Sep 18 '22 11:09

verboze