Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:NvimTreeToggle - toggle file explorer in all tabs #1493

Closed
erikw opened this issue Aug 9, 2022 · 5 comments · Fixed by #1698
Closed

:NvimTreeToggle - toggle file explorer in all tabs #1493

erikw opened this issue Aug 9, 2022 · 5 comments · Fixed by #1698
Labels
feature request PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated QOL Quality Of Life Improvement

Comments

@erikw
Copy link

erikw commented Aug 9, 2022

I would like the file browser to act globally meaning that it's either hidden in all tabs or visible in all tabs.

By setting

open_on_tab = true

I can get halfway there. The problem is that there's no way that I've found to close the open all file explorers.

Is there already a way to do accomplish this (closing all open file explorers)? Otherwise this could be a separate command :NvimTreeToggleGlobally that overrides individual tab statuses and force closes or opens the file explorer in all tabs.

Update

I found that I can make a global close and open by using two different keyboard shortcuts.

noremap <silent> <F2> :NvimTreeOpen<CR>
function! NvimTreeCloseAll()
	let current_tab = tabpagenr()
	tabdo NvimTreeClose
	execute 'tabnext' current_tab
endfunction
" }
nnoremap <silent> <S-F2> :call NvimTreeCloseAll()<CR>

But the best would be a global toggle like described above!

@alex-courtis alex-courtis added the PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated label Aug 14, 2022
@alex-courtis alex-courtis added the QOL Quality Of Life Improvement label Oct 31, 2022
@alex-courtis
Copy link
Member

@jimafisk
Copy link

jimafisk commented May 14, 2024

Thanks so much for the tips @erikw! You actually helped me solve multiple issues 🎉

Setting open_on_tab to true was exactly what I was looking for here (#1880). For other folks, you can keep the file browser open in new tabs (when doing ctrl-t) by simply add the follow to /.config/nvim/init.vim:

lua <<EOF
require("nvim-tree").setup({
  open_on_tab = true,
})
EOF

Your global open / close snippet was great too, I modified it slightly to toggle when typing nt by adding the following to /.config/nvim/init.vim:

function! NvimTreeToggleAll()
  let current_tab = tabpagenr()
  if g:nvim_tree_open
    tabdo NvimTreeClose
    let g:nvim_tree_open = 0
  else
    tabdo NvimTreeOpen
    let g:nvim_tree_open = 1
  endif
  execute 'tabnext' current_tab
endfunction
let g:nvim_tree_open = 0
nnoremap nt :call NvimTreeToggleAll()<CR>

Note that if you open a directory instead of a file, your nvim-tree will already by open but the script above globally sets it as closed initially. So the first time you hit nt it will try to open the already opened file browser and it will look like nothing happened. The second time it should work though. Would be better to figure out how to use a variable to know if nvim-tree is actually open (#505) instead of arbitrarily setting it like I'm doing in the script above.

@jimafisk
Copy link

Quick update: you can use isdirectory(argv(0)) to determine if a folder was opened initially. This will prevent the issue I mentioned above about having to type nt twice when opening a directory instead of a file.

It should evaluate to true ✔️ in the following:

  • nvim .
  • nvim myfolder/
  • nvim myfolder

Should be false ❌ for:

  • nvim
  • nvim myfilename

The full script might look something like:

function! NvimTreeToggleAll()
   let current_tab = tabpagenr()
   if g:nvim_tree_open
      tabdo NvimTreeClose
      let g:nvim_tree_open = 0
   else
      tabdo NvimTreeOpen
      let g:nvim_tree_open = 1
   endif
   execute 'tabnext' current_tab
endfunction
let g:nvim_tree_open = 0
if isdirectory(argv(0))
   let g:nvim_tree_open = 1
endif
nnoremap nt :call NvimTreeToggleAll()<CR>

@alex-courtis
Copy link
Member

Nice! Do you want to share that one as a Recipe?

@jimafisk
Copy link

jimafisk commented Jun 7, 2024

Sounds good to me @alex-courtis! I just added a recipe here: https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes#toggle-nvim-tree-open--closed-with-nt

Just let me know if I did that wrong or if you're looking for something else! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated QOL Quality Of Life Improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants