junegunn / fzf.vim

fzf :heart: vim
MIT License
9.55k stars 583 forks source link

Prioritize Open Buffers when using :Files? #1401

Closed seb-jones closed 1 year ago

seb-jones commented 2 years ago

Hello,

Is it possible to prioritize files that are already open in buffers when using :Files? I often find myself jumping between the same files a lot, so I think it would be a more of an efficient workflow if already-opened files appeared at the start of the list.

I am aware that there is another command :Buffers for viewing only open buffers - however I think it would be preferable not to have two different keybindings, because then you have to try and remember if you had already opened a buffer before trying either :Buffers or :Files.

Many Thanks

Seb

oriming commented 1 year ago

I have the same problem

seb-jones commented 1 year ago

I'm not all that familiar with vimscript but I think I have managed to get roughly what I want by adapting this example from the main FZF repo.

Here's my code:

" FZF Files and Buffers in a single list using Ctrl + P

nnoremap <C-P> :FzfFilesAndBuffers<CR>

command! FzfFilesAndBuffers call fzf#run({
\ 'source':  reverse(s:all_files()),
\ 'sink':    'edit',
\ 'down':    '40%',
\ })

function! s:all_files()
  return extend(
    \ reverse(
      \ filter(
        \ split(
          \ system('fd --type file --type symlink --hidden --no-ignore-vcs --exclude vendor --exclude node_modules --exclude .git'),
          \ "\n"
        \ ),
        \ '!empty(v:val)'
      \ ),
    \ ),
  \ map(filter(range(1, bufnr('$')), 'buflisted(v:val)'), 'bufname(v:val)'))
endfunction

The system() call should be altered to meet the specific needs of the user.

It's not perfect, for instance if a buffer is open it's corresponding file will also show in the list, causing a duplicate, and also the currently opened file will be the first item in the list and can be selected, which isn't very useful. But generally I think it does the job.