vim-fern icon indicating copy to clipboard operation
vim-fern copied to clipboard

Close fern drawer after opening

Open leftbones opened this issue 2 years ago • 10 comments

I'm using nmap <silent> <Leader>f :Fern . -drawer -toggle<CR> to open and close the fern drawer, I'd like it so when I select a file and press t to tabedit, fern will automatically close the drawer before taking me to the new tab.

I tried this: https://github.com/lambdalisue/fern.vim/wiki/Tips#close-fern-after-open

But it did not work.

leftbones avatar Apr 04 '22 01:04 leftbones

I'm sorry but I don't get your situation. How to reproduce the situation and what is actual and expected behavior?

lambdalisue avatar Apr 04 '22 04:04 lambdalisue

@lambdalisue More details:

To reproduce:

  1. Open fern drawer with the mapping I specified in my original post from tab 1
  2. Select a file in the list, press t to tabedit the file (file opens in tab 2 and switches me to tab 2)
  3. Upon going back to tab 1, the fern drawer is still open

The expected behavior is that after opening the file and going to the new tab (tab 2), the fern drawer would be closed automatically so that it is no longer open when I return to tab 1.

leftbones avatar Apr 04 '22 14:04 leftbones

Hm.. It seems the feature request instead of bug. Current fern behavior is expected for me.

@evprkr seems need the new feature to close fern window after open files. For example defx.nvim has the feature like this.

		" auto quit like behavior
		nnoremap <silent><buffer><expr> <CR>
		\ defx#do_action('multi', ['drop', 'quit'])
		nnoremap <silent><buffer><expr> s
		\ defx#do_action('multi', [['drop', 'split'], 'quit'])

Shougo avatar Apr 05 '22 00:04 Shougo

@Shougo I didn't mean this to be a bug report, more of a "How do I do this?" or "Can this be added if it isn't already possible?" type of post.

leftbones avatar Apr 05 '22 02:04 leftbones

I tried this: https://github.com/lambdalisue/fern.vim/wiki/Tips#close-fern-after-open

I think it does not work for tabedit. Can you test it with other open commands? I don't know it is bug or expected behavior.

Shougo avatar Apr 05 '22 02:04 Shougo

I tried this: https://github.com/lambdalisue/fern.vim/wiki/Tips#close-fern-after-open

I think it does not work for tabedit. Can you test it with other open commands? I don't know it is bug or expected behavior.

I just put it back into my .vimrc, closed vim. Reopened, opened the fern drawer, selected a file and pressed Enter to open the file, and the fern drawer remained open. So it might be a bug, or maybe I am doing something wrong.

leftbones avatar Apr 05 '22 18:04 leftbones

Hm. OK. I will try it.

Shougo avatar Apr 06 '22 00:04 Shougo

I have tested below minimal vimrc and it does not work for action-open but it does not work for action-tabopen. I think FernDo close -drawer -stay does not support tab.

set rtp+=~/src/fern.vim

nnoremap <silent> <Space>f :Fern . -drawer -toggle<CR>

function! s:init_fern() abort
  nnoremap <Plug>(fern-close-drawer) :<C-u>FernDo close -drawer -stay<CR>
  nmap <buffer> <Plug>(fern-action-open-and-close)
        \ <Plug>(fern-action-open)<Plug>(fern-close-drawer)
  nmap <buffer> <Plug>(fern-action-tabopen-and-close)
        \ <Plug>(fern-action-open:tabedit)<Plug>(fern-close-drawer)
  nmap <buffer> <CR> <Plug>(fern-action-open-and-close)
  nmap <buffer> t <Plug>(fern-action-tabopen-and-close)
  nmap <buffer> q <Plug>(fern-close-drawer)
endfunction

augroup fern-custom
  autocmd! *
  autocmd FileType fern call s:init_fern()
augroup END

Shougo avatar Apr 06 '22 00:04 Shougo

I just put it back into my .vimrc, closed vim. Reopened, opened the fern drawer, selected a file and pressed Enter to open the file, and the fern drawer remained open. So it might be a bug, or maybe I am doing something wrong.

You need to map <CR> like my config.

Note: I think the documentation should be updated though...

Shougo avatar Apr 06 '22 00:04 Shougo

As @Shougo mentioned, FernDo is for executing a command on a current tab page.

Unfortunately, there is no simple solution for your case. You need to write a lot of Vim script to do that (e.g. save win_getid() before and call win_execute(winid, 'close') or whatever after t)

lambdalisue avatar Apr 06 '22 13:04 lambdalisue

Implementation that works for me:

vim.api.nvim_buf_set_keymap(0, "n", "t", "<Plug>(fern-action-open:tabedit) gt :FernDo close -drawer<CR> gT")

jneidel avatar Jun 17 '23 18:06 jneidel