Skip to content

feat: default to opening last accessed window when window picker is disabled #3321

Open

Description

@mikesmithgh

Describe the solution you'd like
I prefer to have the window_picker disabled and would like the default behavior of selecting a window, when window_picker is disabled, to attempt to use your last accessed window.

Describe alternatives you've considered
I created my own picker function to implement the desired behavior, it is a copy paste of the current nvim-tree logic with a one line change.

My setup to get the desired behavior:

---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids
local function usable_win_ids()
  local full_name = require('nvim-tree.renderer.components.full-name')
  local view = require('nvim-tree.view')
  local config = require('nvim-tree.config')
  local tabpage = vim.api.nvim_get_current_tabpage()
  local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
  local tree_winid = view.get_winnr(tabpage)

  return vim.tbl_filter(function(id)
    local bufid = vim.api.nvim_win_get_buf(id)
    for option, v in pairs(config.g.actions.open_file.window_picker.exclude) do
      local ok, option_value = pcall(vim.api.nvim_get_option_value, option, { buf = bufid })

      if ok and vim.tbl_contains(v, option_value) then
        return false
      end
    end

    local win_config = vim.api.nvim_win_get_config(id)
    return id ~= tree_winid and id ~= full_name.popup_win and win_config.focusable and not win_config.hide and not win_config.external or false
  end, win_ids)
end

return {
  'nvim-tree/nvim-tree.lua',
  enabled = true,
  lazy = true,
  dependencies = {
    'nvim-tree/nvim-web-devicons', -- optional, for file icons
  },
  event = 'VeryLazy',
  cmd = { 'NvimTreeToggle', 'NvimTreeFindFile', 'NvimTreeFocus' },
  config = function()
    require('nvim-tree').setup({
      hijack_netrw = false,
      renderer = {
        hidden_display = 'all',
      },
      actions = {
        open_file = {
          window_picker = {
            picker = function()
              -- logic copied from NvimTree with slight modification
              -- attempt to use last accessed window, otherwise fallback to NvimTree logic
              -- also respect excluded window types
              local usable_wins = usable_win_ids()
              local target_winid = vim.fn.win_getid(vim.fn.winnr('#'))
              -- first available usable window
              if not vim.tbl_contains(usable_wins, target_winid) then
                if #usable_wins > 0 then
                  target_winid = usable_wins[1]
                else
                  target_winid = -1
                end
              end
              return target_winid
            end,
          },
        },
      },
    })
  end,
}

Additional context
Locally, I changed

target_winid = lib.target_winid

from

target_winid = lib.target_winid

to

target_winid = vim.fn.win_getid(vim.fn.winnr('#'))

This gave me the desired results. If this is something you are interested in, I could open a PR. Thanks!

Activity

  1. alex-courtis commented on Apr 25, 2026

    @alex-courtis
    Member

    lib target_winid is some deep tech debt. It's a global which can easily get stale. It's time to fix this. vim.fn.winnr('#') is sensible.

    Please do create a PR to remove and replace target_winid.

    As this is deep legacy, we should use an experimental feature flag (default off) to roll this out over a month or so, with both of us enabling it to test.

    Perhaps nvim_tree.config.experimental.open_in_previous_window_when_not_using_window_picker

  2. mikesmithgh commented on Apr 30, 2026

    @mikesmithgh
    Author

    Sounds good, might take me a little bit of time to get around to doing this but will try to carve out some time 👍

  3. added
    PR please
    regression
    and removed on Apr 30, 2026
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 pleaseregression

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions