Skip to content

Previous window is not selected unless :NvimTreeFocus is used #2262

Open
@zhimsel

Description

@zhimsel

Description

From #1985 (comment)

Is there really no way to open to the window that was previously active before selecting the nvim-tree window (regardless on navigation method)?

I have the same problem as @paulrouget, however I often use the mouse to start interacting with nvim-tree (to open a file, for instance). If I don't use :NvimTreeFocus, then it opens the file in the last window to have previously used :NvimTreeFocus (or presumably have been created by nvim-tree).

Would it be possible to use the lua equivalent of :winnr(#) (if it exists)?

Would it be possible to track the previous window by an event that fires every time you leave a window? Like maybe the WinLeave autocmd event? We could add another WinLeave autocmd similar to the one defined here, but using a negated pattern to ignore unwanted buffers like NvimTree_*.


TL;DR

This plugin can only track the "previous active window" when it's focused via :NvimTreeFocus. If the nvim-tree is focused by any other method, it will still refer to the window where :NvimTreeFocus was lat called, not the actual last window.

Neovim version

NVIM v0.9.0
Build type: Release
LuaJIT 2.1.0-beta3

Operating system and version

Linux 6.2.12-zen1-1-zen, MacOS 13.4

nvim-tree version

Latest master (0345117 as of testing)

Minimal config

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",
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    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

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("nvim-tree").setup({
    actions = {
      open_file = {
        window_picker = {
          enable = false
        },
      },
    },
  })
end

Steps to reproduce

Setup

Open nvim-tree and open at least 2 files in splits (vertical/horizontal doesn't seem to matter). In this example I'll open file1, file2, and file3 in vertical splits in order (1 on the left, 3 on the right).

  1. nvim -nu /tmp/nvt-min.lua
  2. :NvimTreeOpen
  3. Select file1 and press <C-v>
  4. :NvimTreeFocus
  5. Select file2 and press <C-v>
  6. :NvimTreeFocus
  7. Select file3 and press <C-v>

The bug

  1. With window 3 selected (as it should be after the last step above), focus nvim-tree with :NvimTreeFocus
  2. Open another file with <CR>. The file opens in window 3, as it was the last window selected.
  3. Focus window 1 (with file1 open) with any method of your choice (mouse, <C-w>h, whatever)
  4. Focus nvim-tree this time with a method that doesn't involve calling :NvimTreeFocus, like clicking the window with the mouse, or using <C-w>h to move the cursor to the nvim-tree window.
  5. Open a file that is not currently open in a window with <CR>

Expected behavior

I would expect the file to be opened in window 1 (where file1 used to be), as it was the last window selected.

Actual behavior

The file is opened in window 3 (where file3 was), as it was the window selected when :NvimTreeFocus was last called.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions