Skip to content

window_picker.exclude should apply when deciding to create a new window #3364

Open

Description

@nolleh

Description

When NvimTree and windows excluded by actions.open_file.window_picker.exclude are the only windows in a tab, opening a file can replace an excluded window instead of creating a new regular window.

With the default configuration, a window containing a buftype=nofile buffer is excluded from the window picker. When that is the only non-tree window, there are no usable target windows. However, NvimTree falls back to the window from which the tree was opened and opens the file there, replacing the excluded buffer.

This appears to be a remaining edge case related to #3143/#3144: exclusions are applied while collecting usable windows, but an excluded window can still be selected as the fallback when no usable window exists.

Neovim version

NVIM v0.12.5
Build type: Release
LuaJIT 2.1.1788856981

Operating system and version

macOS 26.6.2 arm64

Windows variant

N/A

nvim-tree version

master, commit 355bcc85

Clean room replication

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvt-min/site]])

local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"

local function load_plugins()
  require("packer").startup({
    {
      "wbthomason/packer.nvim",
      "nvim-tree/nvim-tree.lua",
      "nvim-tree/nvim-web-devicons",
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  })
end

if vim.fn.isdirectory(install_path) == 0 then
  print("Installing nvim-tree and dependencies.")
  vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path })
end

load_plugins()
require("packer").sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]])

vim.opt.termguicolors = true
vim.opt.cursorline = true

_G.setup = function()
  require("nvim-tree").setup({})
end

Steps to reproduce

  1. Run nvim -nu /tmp/nvt-min.lua and wait for the plugins to finish installing.
  2. Run :enew.
  3. Run :setlocal buftype=nofile.
  4. Run :NvimTreeOpen.
  5. Select any file and press <CR>.

Expected behavior

The nofile window is excluded and remains unchanged. Because no usable target window exists, NvimTree creates a new regular window and opens the selected file there.

Actual behavior

The selected file replaces the buffer in the excluded nofile window. No new regular window is created.

Activity

  1. alex-courtis commented on Oct 18, 2026

    @alex-courtis
    Member

    I'm able to replicate the behaviour:

    :enew
    :ls!
      1 %a   "[No Name]"                    line 1
    :setlocal buftype=nofile
    :ls!
      1 %a   "[Scratch]"                    line 1
    :NvimTreeOpen
    <open anything>
    :ls!
      1 %a   ".mailmap"                     line 1
      2u a-  "NvimTree_1"                   line 0
    

    If there is text in the scratch buffer it won't be wiped:

    :enew
    <insert some text>
    :ls!
      1 %a + "[No Name]"                    line 1
    :setlocal buftype=nofile
    :ls!
      1 %a   "[Scratch]"                    line 1
    :NvimTreeOpen
    <open anything>
    :ls!
      1 #h   "[Scratch]"                    line 1
      2u a-  "NvimTree_1"                   line 0
      3 %a   ".mailmap"                     line 1
    

    It may be Nvim itself that decides to wipe the empty scratch buffer.

    This test indicates that I'm not understanding or replicating the issue properly.

    Could you please provide another reproducer that uses, say, filetype="yaml", removing any ambiguity around empty scratch buffers.

  2. nolleh commented on Oct 19, 2026

    @nolleh
    Author

    Thanks. I was able to reproduce the same behaviour without using a
    scratch buffer.

    window_picker.exclude can be configured to exclude windows displaying
    a YAML buffer:

    require("nvim-tree").setup({
      actions = {
        open_file = {
          window_picker = {
            exclude = {
              filetype = { "yaml" },
            },
          },
        },
      },
    })

    Create two non-empty files:

    mkdir -p /tmp/nvt-window-picker-repro
    printf 'keep: this-window\n' > /tmp/nvt-window-picker-repro/excluded.yaml
    printf 'open me\n' > /tmp/nvt-window-picker-repro/target.txt

    Steps to reproduce:

    1. Open /tmp/nvt-window-picker-repro/excluded.yaml.
    2. Run :set filetype=yaml to ensure the buffer is excluded.
    3. Run :NvimTreeOpen /tmp/nvt-window-picker-repro.
    4. Select target.txt in NvimTree and press <CR>.

    Before opening target.txt, the tab contains two windows:

    • NvimTree
    • excluded.yaml

    Expected result:

    • The window containing excluded.yaml remains unchanged.
    • A new regular window is created for target.txt.

    Actual result:

    • The window containing excluded.yaml is reused for target.txt.
    • The YAML buffer remains loaded but becomes hidden.
    • The tab still contains only two windows: NvimTree and target.txt.

    This shows that the issue is not caused by Neovim wiping or reusing an
    empty scratch buffer. The excluded YAML window is still selected as the
    fallback target when no usable window is available.

  3. alex-courtis commented on Oct 19, 2026

    @alex-courtis
    Member
    • The window containing excluded.yaml remains unchanged.
    • A new regular window is created for target.txt.

    That is expected behaviour. nvim-tree will open the file in the only available window: excluded.yaml. window_picker.exclude is not applicable.

    window_picker.exclude applies only to the case of multiple avaliable windows, see :help nvim_tree.config.actions.open_file.window_picker

    Demo:

    • add 2 more files one and two
    • open windows with 2 above and excluded.yaml
      • :e one
      • <C-W>v
      • :e two
      • <C-W>v
      • :e excluded.yaml`
      • :NvimTreeOpen
      • open target.txt
    • the picker will appear for one and two but not excluded.yaml windows
    Image
  4. alex-courtis commented on Oct 19, 2026

    @alex-courtis
    Member

    To achieve the functionality you are after, you will need to use a picker function that decides which window should open the file. See :help nvim_tree.config.actions.open_file.window_picker

    Skeleton:

          window_picker = {
            enable = true,
            picker = function()
              for _, winid in ipairs(vim.api.nvim_list_wins()) do
                print("picker function winid = " .. winid)
                local bufnr = vim.api.nvim_win_get_buf(winid)
                print("                bufnr = " .. bufnr)
                local ft = vim.api.nvim_get_option_value("filetype", { buf = bufnr })
                print("                ft    = " .. ft)
              end
              return 1000
            end,
  5. nolleh commented on Nov 14, 2026

    @nolleh
    Author

    @alex-courtis

    Thanks for the clarification. I understand now that, in the default
    picker flow, exclude does not prevent the only non-tree window from
    being used as the fallback target.

    I implemented the desired behaviour with a custom picker:

    • use NvimTree's configured window_picker.exclude values;
    • use the eligible window directly when there is only one;
    • preserve the A / B picker when there are multiple eligible windows;
    • create a new regular editor window when there are no eligible windows.

    For anyone with a similar use case, the implementation is here:

    nolleh/dotfiles@5ca9914

    One small documentation suggestion: it may be helpful to explicitly mention
    that exclude does not prevent the sole non-tree window from being used as
    the fallback target, and that a custom picker is required to create a new
    window in that case. I initially interpreted exclude as a stronger
    guarantee that matching windows would never be used as file-open targets.

    This resolves my use case, so please feel free to close the issue.

  6. alex-courtis commented on Nov 15, 2026

    @alex-courtis
    Member

    For anyone with a similar use case, the implementation is here:

    nolleh/dotfiles@5ca9914

    That's fantastic! Please share this on the Recipes page for others to find and use.

    One small documentation suggestion: it may be helpful to explicitly mention that exclude does not prevent the sole non-tree window from being used as the fallback target, and that a custom picker is required to create a new window in that case. I initially interpreted exclude as a stronger guarantee that matching windows would never be used as file-open targets.

    Is this extra text in the exclude field clear?

    *nvim_tree.config.actions.open_file.window_picker.exclude*
        Tables of buffer option names mapped to a list of option values. Windows
        containing matching buffers will not be:
        • available when using a window picker
        • selected when not using a window picker
    
    This applies only when using a window picker: when there is only one window available, the file will be opened in that window even if it matches {exclude}
    
        Fields: ~
          • {buftype}?   (`string[]`) (default:
                         `{ "nofile", "terminal", "help", }`)
          • {filetype}?  (`string[]`) (default:
                         `{ "notify", "lazy", "qf", "diff", "fugitive", "fugitiveblame", }`)
  7. alex-courtis commented on Nov 16, 2026

    @alex-courtis
    Member

    Thinking more on this issue and your solution, I think we can and should add it to nvim-tree. It could be a new option open_file.new_window, something like:

        open_file = {
          new_window = {
            filetype = { "yaml" },
            buftype = { "terminal", },
          },
          ---
          window_picker = {
            enable = true,
            picker = "default",
            chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
            exclude = {
              filetype = { "notify", "packer", "qf", "diff", "fugitive", "fugitiveblame" },
              buftype = { "nofile", "terminal", "help" },
            },
          },
        },

    Are you open to creating a pull request to add this feature?

  8. added
    PR please
    QOL
    on Nov 16, 2026
  9. nolleh commented on Nov 17, 2026

    @nolleh
    Author

    Thanks, I’d be happy to work on a pull request.

    Before implementation, I’d like to clarify the intended semantics of
    open_file.new_window. I may be interpreting the proposal differently.

    My custom picker treats this as the fallback behaviour for an empty set
    of eligible windows:

    1. Apply window_picker.exclude.
    2. If multiple eligible windows remain, use the existing picker.
    3. If exactly one remains, use it directly.
    4. If none remain, create a regular editor window instead of falling
      back to an excluded window.
    Eligible editor windows Behaviour
    Multiple Show the existing A / B picker
    One Open the file in that window
    None Create a new regular window

    The existing NvimTree-only case is why this feels natural to me:

    NvimTree only
        -> no usable editor target
        -> create an editor window
    
    NvimTree + excluded windows only
        -> no usable editor target after applying `exclude`
        -> create an editor window
    

    This could be represented as a fallback policy, for example:

    window_picker = {
      fallback = "new_window",
    }

    The current remembered-window behaviour could remain the default for
    backwards compatibility.

    My concern with a second table of buffer option values is that the
    original use case may require duplicating the same policy:

    open_file = {
      new_window = {
        buftype = { "nofile", "terminal", "help" },
      },
      window_picker = {
        exclude = {
          buftype = { "nofile", "terminal", "help" },
        },
      },
    }

    exclude would define which windows are not eligible targets, while
    new_window would need to repeat those values to prevent the same
    windows from being used as fallback targets.

    If new_window is also intended to apply when the user explicitly
    selects a window from the multi-window picker, that seems like a
    separate behaviour. Selecting a window currently means “open the file
    in this window”, so changing that to “create a new window next to this
    window” could be surprising.

    Would a zero-candidate fallback option better match the intended
    feature, or is that broader selected-window behaviour intentional?

  10. alex-courtis commented on Nov 17, 2026

    @alex-courtis
    Member

    My concern with a second table of buffer option values is that the original use case may require duplicating the same policy:

    That is sensible. open_file.new_window is too complex in implementation and from a user perspective. The multiple-one-zero fallback approach is intuitive and should cover all cases.

    This could be represented as a fallback policy, for example:

    window_picker = {
    fallback = "new_window",
    }
    The current remembered-window behaviour could remain the default for backwards compatibility.

    This approach is safe and should be reasonable to implement.

    Would a zero-candidate fallback option better match the intended feature, or is that broader selected-window behaviour intentional?

    Honestly, the intended behaviour is not documented and not really known; it evolved during initial implementation many years ago and with many bug fixes.

    Let's make a decision after implementation: keep the window_picker.fallback option or roll it out as an experiment which will become the new default.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    PR pleaseQOLawaiting feedback

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions