fzf.vim icon indicating copy to clipboard operation
fzf.vim copied to clipboard

Switch between buffer mode and file mode while keeping context

Open andersjanmyr opened this issue 6 years ago • 5 comments

  • Category
    • [x] Question
    • [ ] Bug
    • [x] Suggestion
  • OS
    • [x] Linux
    • [x] macOS
    • [x] Windows
    • [x] Etc.
  • Vim
    • [x] Vim
    • [x] Neovim

I often start out by searching for a file with :Buffers and then realize that I don't have a buffer open for the file I'm looking for. I would like to be able switch to :Files mode without loosing my context (with, for example, ctrl-f). Is this possible, if so, how? If it is not possible, wouldn't this be a cool feature?

andersjanmyr avatar Oct 27 '17 22:10 andersjanmyr

I have this for switching fzf commands (the context is not saved, but I think could be done by saving the last inserted text)

" ## FZF


let g:fzf_command_prefix = 'Fz'
let g:fzf_commands_expect = 'alt-enter'
let g:fzf_history_dir = '~/.local/share/fzf-history'


nnoremap <C-p> :FzFiles<CR>


function! s:set_fzf_maps()
  tnoremap <buffer> <C-t> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzFiles<CR>
  tnoremap <buffer> <C-f> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzBuffers<CR>
  tnoremap <buffer> <C-s> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzGFiles?<CR>
  tnoremap <buffer> <C-g> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzAg<CR>
  tnoremap <buffer> <C-l> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzBLines<CR>
  tnoremap <buffer> <C-o> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzHistory<CR>
  tnoremap <buffer> <C-c> <C-\><C-n>:close<CR>:sleep 100m<CR>:FzCommands<CR>
endfunction


augroup fzfMappingsAu
  autocmd!
  autocmd FileType fzf call <SID>set_fzf_maps()
augroup END

Note: I had to use the :sleep 100m hack here if not the cursor does not move to the new buffer (I'm using Neovim, maybe is that?).

stsewd avatar Mar 18 '18 06:03 stsewd

@stsewd Thanks, this helps.

andersjanmyr avatar Mar 18 '18 20:03 andersjanmyr

Any advice how how easily to save the inserted text?

nyarly avatar Sep 13 '18 18:09 nyarly

I'd also be curious to know how to save the inserted text

satshabad-cr avatar Sep 26 '19 22:09 satshabad-cr

I got close to implementing this like so:

command! -bang -nargs=? -complete=dir Buffers
    \ call fzf#vim#buffers(<q-args>, {'options': [
    \  '--bind',
    \  'ctrl-f:reload(' . $FZF_DEFAULT_COMMAND . '| sed "s/.*/\t\0\t\0/")+' .
    \  'change-prompt(Files> )'
    \ ]}, <bang>0)

The sed is necessary because the :Buffers command passes -d '\t' --with-nth '3..' -n '2,1..2' to fzf.

It works nicely to select the file, but the file doesn't actually open properly because s:bufopen() is expecting to open an existing buffer number.

jonleighton avatar Jul 25 '21 11:07 jonleighton