fzf.vim
fzf.vim copied to clipboard
Switch between buffer mode and file mode while keeping context
- 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?
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 Thanks, this helps.
Any advice how how easily to save the inserted text?
I'd also be curious to know how to save the inserted text
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.