junegunn / fzf.vim

fzf :heart: vim
MIT License
9.59k stars 582 forks source link

Always open the selected file in the editor pane even the file explorer pane is on focus #1359

Closed ranelpadon closed 2 years ago

ranelpadon commented 2 years ago

System Mac/Neovim

Issue Sometimes I forgot that I'm on the file explorer pane/buffer:

Screen Shot 2022-01-23 at 2 30 57 AM

and then searched a file (:Files) or code pattern (:Rg), then open the selected file (using the Enter key). But the file will be open in the file explorer pane, I think it's because it's the currently active "buffer". However, this will mess up the existing window layout since the file explorer pane will be gone and be replaced by the selected file. This doesn't look good also since the file explorer pane usually is narrower, so the opened file will be "cramped" into it:

Screen Shot 2022-01-23 at 2 31 24 AM

Ideally, the selected file should open in the editor pane even the file explorer pane has the focus. It's the default behavior in other editors/IDEs (i.e. the selected file will always open in the editor pane regardless where the current focus is).

Likewise, the file explorer pane/buffer doesn't show in the Buffers list, so it's not treated as a usual file buffer, and hence could be differentiated. So, I'm thinking if there's a way to always open in the editor pane/buffers list? This is a related setting, but seems didn't address this issue/behavior?

" [Buffers] Jump to the existing window if possible
let g:fzf_buffers_jump = 1

My window layout is always like that, that is, toggleable file explorer on the left (which is open most of the time), and then editor pane on the right. No more other splits. FZF is rendered on the floating window.

The same behavior happens even when using CoC-Explorer, or barebones nvim with netrw file explorer (:Explore). This could be the default behavior of Vim/Neovim, but maybe FZF.vim could add this feature, via global setting or maybe some tips on how to implement this on my own via script/hook if you're super busy. Thanks :)

ranelpadon commented 2 years ago

This seems to work. Instead of the usual:

map <Leader>p :Files<CR>

modified my mapping to shift window focus first before executing FZF commands (wincmd l to always shift the focus to the right window):

map <Leader>p :wincmd l <Bar> :Files<CR>

But I wonder if there's a better way.

junegunn commented 2 years ago

There are many file explorer plugins (netrw, nerdtree, coc-explorer, vim-dirvish, fern.vim, to name a few), but unfortunately, there is no standardized way to tell if a buffer window is a sidebar that is not supposed to open files in. The sidebars these plugins implement look like a sidebar in other IDEs, but they're just narrow split windows.

Also, there are times you do want to open files in these file explorer buffers (e.g. 1. Open vim, 2. split ., 3. :Files, 4. Open a file in that split).

junegunn commented 2 years ago

You can try something like this; if you're on a window with a nofile buffer, and the size of the window is small relative to the screen, move to the previous window.

command! -bar MoveBack if &buftype == 'nofile' && (winwidth(0) < &columns / 3 || winheight(0) < &lines / 3) | execute "normal! \<c-w>\<c-p>" | endif
nnoremap <silent> <Leader><Leader> :MoveBack<BAR>Files<CR>
nnoremap <silent> <Leader><Enter>  :MoveBack<BAR>Buffers<CR>
ranelpadon commented 2 years ago

Good point on the lack of standard implementation on sidebars, and thanks for your suggestion. I ended up just wrapping the FZF functions:

function ShiftFocusThenExecute(command)
    " Shift focus to the right/main window,
    " especially when focus is in sidebar.
    :wincmd l

    " Run commands like `:Files`.
    execute a:command
endfunction

nnoremap <Leader>p :call ShiftFocusThenExecute('Files')<CR>
nnoremap <Leader>rg :call ShiftFocusThenExecute('Rg')<CR>
nnoremap <Leader>b :call ShiftFocusThenExecute('Buffers')<CR>
nnoremap <Leader>m :call ShiftFocusThenExecute('Maps')<CR>
...

I think I'll just close this since it's not really a bug in FZF. Cheers