Skip to content

BUG: :Neotree toggle/reveal don't focus neo-tree window when switching source #2016

@Wikiwix

Description

@Wikiwix

Did you check docs and existing issues?

  • I have read all the docs.
  • I have searched the existing issues.
  • I have searched the existing discussions.

Neovim Version (nvim -v)

NVIM v0.12.1

Operating System / Version

Windows 11 25H2 / Ubuntu 24.04.4 LTS

Describe the Bug

When Neotree is open in a window with source A (e.g. buffers) and the cursor is currently in another window, a :Neotree toggle filesystem does switch the source to filesystem`, but does not bring that window in focus (i.e. move the cursor there).

The same is true for :Neotree reveal.


The following change fixes it for me (with all tests still passing and based on my testing other behaviour staying as expected) and I can create a PR with that change, but I am not entirely sure whether manager.navigate is actually expected to always navigate to state.winid (a call a few lines above somewhat indicates that).

diff --git a/lua/neo-tree/command/init.lua b/lua/neo-tree/command/init.lua
index 3c3145a..0a7a688 100644
--- a/lua/neo-tree/command/init.lua
+++ b/lua/neo-tree/command/init.lua
@@ -48,7 +48,11 @@ local function do_show_or_focus(args, state, force_navigate)
     end
     if force_navigate or not window_exists then
       -- close_other_sources()
-      manager.navigate(state, args.dir, args.reveal_file, nil, false)
+      manager.navigate(state, args.dir, args.reveal_file, function()
+        if state.winid and vim.api.nvim_win_is_valid(state.winid) then
+          vim.api.nvim_set_current_win(state.winid)
+        end
+      end, false)
     end
   end
 end

Screenshots, Traceback

No response

Steps to Reproduce

  1. Open Neovim (config see below)
  2. :Neotree reveal filesystem
  3. <C-w>l (move to another window)
  4. :Neotree toggle buffers or :Neotree reveal buffers (the buffer displays the buffers, but it the cursor is not in the neotree window)

Expected Behavior

The Neotree buffer gets focus

Your Configuration

-- template from https://lazy.folke.io/developers#reprolua, feel free to replace if you have your own minimal init.lua
vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    {
      "nvim-neo-tree/neo-tree.nvim",
      branch = "main"
      dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
        "MunifTanjim/nui.nvim",
      },
      lazy = false,
      ---@module "neo-tree"
      ---@type neotree.Config?
      opts = {},
    }
  },
})
vim.g.mapleader = " "
vim.keymap.set("n", "<leader>e", "<Cmd>Neotree<CR>")
-- do anything else you need to do to reproduce the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions