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

Keep tree open in all tabs #1880

Closed
maxnoe opened this issue Jan 3, 2023 · 15 comments
Closed

Keep tree open in all tabs #1880

maxnoe opened this issue Jan 3, 2023 · 15 comments

Comments

@maxnoe
Copy link

maxnoe commented Jan 3, 2023

Can this functionality be implemented utilising API?

Maybe, but I didn't find a way to detect if the tree is already open in another tab, so my idea of having a an autocmd on TabEnter did not work out.

Is your feature request related to a problem? Please describe.

I am basically looking for the same functionality as provided by https://github.com/jistr/vim-nerdtree-tabs for nerdtree.

Open the file tree ones, have it open in all tabs. When closing, have it close in all tabs. When having a tree open in one tab, creating a new tab or entering an existing tab without the tree open currently, there is no tree in the new tab. The other way around as well: closing the tree in one tab and then switching to one where it was open will still have it open.

Describe the solution you'd like

Be able to have the tree open/close/toggle for all tabs, including new tabs. Not sure what the best way of implementing is. Could be done minimally with new api reporting the current state or a top-level option for this behavior.

@gegoune
Copy link
Collaborator

gegoune commented Jan 3, 2023

Would an auto command on event such as TabEnter work? It could check if tree is opened for tab and open it if not. Would rather not add new option if behaviour can be easily achieved otherwise. I will try to come up with something when I get some time.

@maxnoe
Copy link
Author

maxnoe commented Jan 3, 2023

Would an auto command on event such as TabEnter work? It could check if tree is opened for tab and open it if not.

That was my initial idea, but I couldn't find a way to get the state of the tree (only open it, if it is already open in another tab, close it if it is closed in the last active tab)

@maxnoe
Copy link
Author

maxnoe commented Jan 3, 2023

I could get it to somewhat work using:

local nt_api = require("nvim-tree.api")
local tree_open = false
local function tab_enter()
    if tree_open then
        nt_api.tree.open()
    else
        nt_api.tree.close()
    end
end
nt_api.events.subscribe(nt_api.events.Event.TreeOpen, function() tree_open=true end)
nt_api.events.subscribe(nt_api.events.Event.TreeClose, function() tree_open=false end)
api.nvim_create_autocmd("TabEnter", {callback=tab_enter})

Remaining issues where I would be happy for some guidance:

  • The Tree is focused when switching tabs, I'd prefer the actual buffer to be focused (or at least the last active buffer)
  • For some reason, the TabEnter event is not triggered when opening the tab from nvim-tree (Ctrl+T on a file).

@gegoune
Copy link
Collaborator

gegoune commented Jan 3, 2023

Nice! Perhaps we could add an api returning state. @alex-courtis

The Tree is focused when switching tabs, I'd prefer the actual buffer to be focused (or at least the last active buffer)

Try detecting if nvim-tree is active, which it might always be, not sure, after switching tabs, and run wincmd p to switch to previous window (not sure if it will switch back to prev tab or to previous window of the tab - but even if that's the case it won't work for new tabs), or switch to next window left/right (depending on your configuration).

For some reason, the TabEnter event is not triggered when opening the tab from nvim-tree (Ctrl+T on a file).

Try also TabNewEntered.

@maxnoe
Copy link
Author

maxnoe commented Jan 3, 2023

Try also TabNewEntered

TabNewEnter works, nice!

wincmd p

This also works, great.

This does what I want:

local nt_api = require("nvim-tree.api")
local tree_open = false
local function tab_enter()
    if tree_open then
        nt_api.tree.open()
        api.nvim_command("wincmd p")
    else
        nt_api.tree.close()
    end
end
nt_api.events.subscribe(nt_api.events.Event.TreeOpen, function() tree_open=true end)
nt_api.events.subscribe(nt_api.events.Event.TreeClose, function() tree_open=false end)
api.nvim_create_autocmd("TabEnter,TabNewEnter", {callback=tab_enter})

@maxnoe maxnoe closed this as completed Jan 3, 2023
@gegoune
Copy link
Collaborator

gegoune commented Jan 3, 2023

Awesome! Would you mind adding it to wiki?

@alex-courtis
Copy link
Member

Nice! Perhaps we could add an api returning state. @alex-courtis

We could do something generic like exposing

function M.is_nvim_tree_buf(bufnr)

@bigzhu
Copy link

bigzhu commented Jan 26, 2023

Is there a way to avoid opening same file duplicate tabs when a file is opened in a tab, and instead switch to the already open tab like coc-explorer does?

@alex-courtis
Copy link
Member

Is there a way to avoid opening same file duplicate tabs when a file is opened in a tab, and instead switch to the already open tab like coc-explorer does?

That is standard vim behaviour: :e <buffer visible in another tab's window> will open it in the current tab's window.

You can build this yourself. Something like:

  local function open_switch_tab(node)
    local api = require("nvim-tree.api")
    if node.absolute_path is open in another tab then
      switch to that tab
    end
    api.node.open.edit(node)
  end

...
   { key = "o", action = "open_switch_to_tab", action_cb = open_switch_tab },

@alex-courtis
Copy link
Member

@vinoff
Copy link

vinoff commented Aug 11, 2023

Try also TabNewEntered

TabNewEnter works, nice!

wincmd p

This also works, great.

This does what I want:

local nt_api = require("nvim-tree.api")
local tree_open = false
local function tab_enter()
    if tree_open then
        nt_api.tree.open()
        api.nvim_command("wincmd p")
    else
        nt_api.tree.close()
    end
end
nt_api.events.subscribe(nt_api.events.Event.TreeOpen, function() tree_open=true end)
nt_api.events.subscribe(nt_api.events.Event.TreeClose, function() tree_open=false end)
api.nvim_create_autocmd("TabEnter,TabNewEnter", {callback=tab_enter})

This does not work for me. I am using it lazy.nvim and am placing it in nvim-tree.lua, as with all the other n vim-tree related configs: This is what I get:

attempt to index global 'api' (a nil value)

It seems like it is an undefined variable.

@alex-courtis
Copy link
Member

api.nvim_create_autocmd should be vim.api.nvim_create_autocmd

@jimafisk
Copy link

I'm confused how to use this as well, should I be able to put this in my ~/.config/nvim/init.vim if I wrap it in lua <<EOF and EOF?

If I make the vim.api.nvim_create_autocmd adjustment, for me it still throws:

E5108: Error executing lua [string ":lua"]:85: Invalid 'event': 'TabEnter,TabNewEnter'
stack traceback:
        [C]: in function 'nvim_create_autocmd'
        [string ":lua"]:85: in main chunk

Thank you!

@gegoune
Copy link
Collaborator

gegoune commented May 14, 2024

:h nvim_create_autocmd:

Parameters: ~
 • {event}  (string|array) Event(s) that will trigger the handler
           (`callback` or `command`).

Try to make list of events a table: {'TabEnter', 'TabNewEnter'}

@jimafisk
Copy link

I ended up doing something a little different that accomplishes the same functionality: #1493 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants